From 59403047303a6d7153503abebb6051bbaad770a1 Mon Sep 17 00:00:00 2001 From: neargle Date: Fri, 6 Sep 2024 00:38:59 +0800 Subject: [PATCH 1/4] add utils(kubelet): util only, add kubelet api response --- pkg/util/common.go | 15 +++ pkg/util/kubelet_api.go | 209 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 pkg/util/kubelet_api.go diff --git a/pkg/util/common.go b/pkg/util/common.go index 0fd5615..4e638db 100644 --- a/pkg/util/common.go +++ b/pkg/util/common.go @@ -144,3 +144,18 @@ func IntContains(s []int, e int) bool { } return false } + +// DistinctArr distinct +func DistinctStrArr(s []string) []string { + distinctMap := make(map[string]bool) + var result []string + + for _, item := range s { + if _, exists := distinctMap[item]; !exists { + distinctMap[item] = true + result = append(result, item) + } + } + + return result +} diff --git a/pkg/util/kubelet_api.go b/pkg/util/kubelet_api.go new file mode 100644 index 0000000..eac4255 --- /dev/null +++ b/pkg/util/kubelet_api.go @@ -0,0 +1,209 @@ + +/* +Copyright 2022 The Authors of https://github.com/CDK-TEAM/CDK . + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "time" +) + +type PodList struct { + Kind string `json:"kind"` + APIVersion string `json:"apiVersion"` + Metadata Metadata `json:"metadata"` + Items []Pod `json:"items"` +} + +type Metadata struct { + Name string `json:"name,omitempty"` + GenerateName string `json:"generateName,omitempty"` + Namespace string `json:"namespace,omitempty"` + UID string `json:"uid,omitempty"` + ResourceVersion string `json:"resourceVersion,omitempty"` + CreationTimestamp time.Time `json:"creationTimestamp,omitempty"` + Labels map[string]string `json:"labels,omitempty"` + Annotations map[string]string `json:"annotations,omitempty"` +} + +type OwnerReference struct { + APIVersion string `json:"apiVersion"` + Kind string `json:"kind"` + Name string `json:"name"` + UID string `json:"uid"` + Controller bool `json:"controller"` + BlockOwnerDeletion bool `json:"blockOwnerDeletion"` +} + +type ManagedField struct { + Manager string `json:"manager"` + Operation string `json:"operation"` + APIVersion string `json:"apiVersion"` + Time time.Time `json:"time"` + FieldsType string `json:"fieldsType"` + FieldsV1 FieldsV1 `json:"fieldsV1"` +} + +type FieldsV1 struct { + Metadata struct { + GenerateName string `json:"generateName"` + Labels map[string]string `json:"labels"` + OwnerRefs map[string]string `json:"ownerReferences"` + } `json:"metadata"` + Spec struct { + Affinity map[string]interface{} `json:"affinity"` + Containers map[string]interface{} `json:"containers"` + RestartPolicy string `json:"restartPolicy"` + SchedulerName string `json:"schedulerName"` + SecurityContext struct { + Sysctls []Sysctl `json:"sysctls"` + } `json:"securityContext"` + } `json:"spec"` +} + +type Sysctl struct { + Name string `json:"name"` + Value string `json:"value"` +} + +type Pod struct { + Metadata Metadata `json:"metadata"` + Spec PodSpec `json:"spec"` + Status PodStatus `json:"status"` + ManagedFields []ManagedField `json:"managedFields"` +} + +type PodSpec struct { + Containers []Container `json:"containers"` + RestartPolicy string `json:"restartPolicy"` + TerminationGracePeriodSeconds int `json:"terminationGracePeriodSeconds"` + DNSPolicy string `json:"dnsPolicy"` + ServiceAccountName string `json:"serviceAccountName"` + NodeName string `json:"nodeName"` + SecurityContext SecurityContext `json:"securityContext"` + Affinity Affinity `json:"affinity"` + Tolerations []Toleration `json:"tolerations"` + SchedulerName string `json:"schedulerName"` +} + +type SecurityContext struct { + Sysctls []Sysctl `json:"sysctls"` +} + +type Affinity struct { + NodeAffinity NodeAffinity `json:"nodeAffinity"` +} + +type NodeAffinity struct { + RequiredDuringSchedulingIgnoredDuringExecution struct { + NodeSelectorTerms []NodeSelectorTerm `json:"nodeSelectorTerms"` + } `json:"requiredDuringSchedulingIgnoredDuringExecution"` +} + +type NodeSelectorTerm struct { + MatchFields []MatchField `json:"matchFields"` +} + +type MatchField struct { + Key string `json:"key"` + Operator string `json:"operator"` + Values []string `json:"values"` +} + +type Container struct { + Name string `json:"name"` + Image string `json:"image"` + Ports []Port `json:"ports"` + Env []EnvVar `json:"env"` + Resources Resources `json:"resources"` + SecurityContext SecurityContext `json:"securityContext"` +} + +type Port struct { + Name string `json:"name"` + HostPort int `json:"hostPort"` + ContainerPort int `json:"containerPort"` + Protocol string `json:"protocol"` +} + +type EnvVar struct { + Name string `json:"name"` + Value string `json:"value"` +} + +type Resources struct{} + +type PodStatus struct { + Phase string `json:"phase"` + Conditions []Condition `json:"conditions"` + HostIP string `json:"hostIP"` + PodIP string `json:"podIP"` + PodIPs []PodIP `json:"podIPs"` + StartTime time.Time `json:"startTime"` + ContainerStatuses []ContainerStatus `json:"containerStatuses"` + QOSClass string `json:"qosClass"` +} + +type Condition struct { + Type string `json:"type"` + Status string `json:"status"` + LastProbeTime time.Time `json:"lastProbeTime"` + LastTransitionTime time.Time `json:"lastTransitionTime"` +} + +type PodIP struct { + IP string `json:"ip"` +} + +type ContainerStatus struct { + Name string `json:"name"` + State ContainerState `json:"state"` + LastState ContainerState `json:"lastState"` + Ready bool `json:"ready"` + RestartCount int `json:"restartCount"` + Image string `json:"image"` + ImageID string `json:"imageID"` + ContainerID string `json:"containerID"` + Started bool `json:"started"` +} + +type ContainerState struct { + Running *ContainerStateRunning `json:"running,omitempty"` + Terminated *ContainerStateTerminated `json:"terminated,omitempty"` + Waiting *ContainerStateWaiting `json:"waiting,omitempty"` +} + +type ContainerStateRunning struct { + StartedAt time.Time `json:"startedAt"` +} + +type ContainerStateTerminated struct { + ExitCode int `json:"exitCode"` + Reason string `json:"reason"` +} + +type ContainerStateWaiting struct { + Reason string `json:"reason"` +} + +type Toleration struct { + Key string `json:"key"` + Operator string `json:"operator"` + Effect string `json:"effect"` + TolerationSeconds *int64 `json:"tolerationSeconds,omitempty"` +} + + From fdd8dd7df2bd1d6cd9d90a17a11aef1c42a661e5 Mon Sep 17 00:00:00 2001 From: neargle Date: Mon, 28 Oct 2024 22:24:34 +0800 Subject: [PATCH 2/4] add util: new KubeConfig struct --- pkg/util/kubectl.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pkg/util/kubectl.go b/pkg/util/kubectl.go index fd65b47..08cb8ce 100644 --- a/pkg/util/kubectl.go +++ b/pkg/util/kubectl.go @@ -1,5 +1,45 @@ package util +type KubeConfig struct { + APIVersion string `yaml:"apiVersion"` + Clusters []Cluster `yaml:"clusters"` + Contexts []Context `yaml:"contexts"` + CurrentContext string `yaml:"current-context"` + Kind string `yaml:"kind"` + Preferences struct{} `yaml:"preferences"` + Users []User `yaml:"users"` +} + +type Cluster struct { + Cluster ClusterInfo `yaml:"cluster"` + Name string `yaml:"name"` +} + +type ClusterInfo struct { + CertificateAuthorityData string `yaml:"certificate-authority-data"` + Server string `yaml:"server"` +} + +type Context struct { + Context ContextInfo `yaml:"context"` + Name string `yaml:"name"` +} + +type ContextInfo struct { + Cluster string `yaml:"cluster"` + User string `yaml:"user"` +} + +type User struct { + Name string `yaml:"name"` + User UserInfo `yaml:"user"` +} + +type UserInfo struct { + ClientCertificateData string `yaml:"client-certificate-data"` + ClientKeyData string `yaml:"client-key-data"` +} + func RunKubectlCmd(args ...string) (string, error) { var stdoutStr string var err error From fcea8e9a59d077e001a14ce00519ccd71ad9bbe6 Mon Sep 17 00:00:00 2001 From: neargle Date: Mon, 28 Oct 2024 22:26:12 +0800 Subject: [PATCH 3/4] add util: new KubeConfig struct and K8sPod struct --- pkg/util/k8s.go | 31 +++++++++++++++++++++++++++++++ pkg/util/kubectl.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 pkg/util/k8s.go diff --git a/pkg/util/k8s.go b/pkg/util/k8s.go new file mode 100644 index 0000000..e1e7eb4 --- /dev/null +++ b/pkg/util/k8s.go @@ -0,0 +1,31 @@ +package util + +type K8sPod struct { + APIVersion string `yaml:"apiVersion"` + Kind string `yaml:"kind"` + Metadata K8sObjectMeta `yaml:"metadata"` + Spec K8sPodSpec `yaml:"spec"` +} + +type K8sObjectMeta struct { + Name string `yaml:"name"` + Namespace string `yaml:"namespace,omitempty"` + Labels map[string]string `yaml:"labels,omitempty"` +} + +type K8sPodSpec struct { + Containers []K8sContainer `yaml:"containers"` +} + +type K8sContainer struct { + Name string `yaml:"name"` + Image string `yaml:"image"` + Ports []K8sContainerPort `yaml:"ports,omitempty"` + Command []string `yaml:"command,omitempty"` +} + +type K8sContainerPort struct { + ContainerPort int `yaml:"containerPort"` +} + + diff --git a/pkg/util/kubectl.go b/pkg/util/kubectl.go index fd65b47..08cb8ce 100644 --- a/pkg/util/kubectl.go +++ b/pkg/util/kubectl.go @@ -1,5 +1,45 @@ package util +type KubeConfig struct { + APIVersion string `yaml:"apiVersion"` + Clusters []Cluster `yaml:"clusters"` + Contexts []Context `yaml:"contexts"` + CurrentContext string `yaml:"current-context"` + Kind string `yaml:"kind"` + Preferences struct{} `yaml:"preferences"` + Users []User `yaml:"users"` +} + +type Cluster struct { + Cluster ClusterInfo `yaml:"cluster"` + Name string `yaml:"name"` +} + +type ClusterInfo struct { + CertificateAuthorityData string `yaml:"certificate-authority-data"` + Server string `yaml:"server"` +} + +type Context struct { + Context ContextInfo `yaml:"context"` + Name string `yaml:"name"` +} + +type ContextInfo struct { + Cluster string `yaml:"cluster"` + User string `yaml:"user"` +} + +type User struct { + Name string `yaml:"name"` + User UserInfo `yaml:"user"` +} + +type UserInfo struct { + ClientCertificateData string `yaml:"client-certificate-data"` + ClientKeyData string `yaml:"client-key-data"` +} + func RunKubectlCmd(args ...string) (string, error) { var stdoutStr string var err error From 09ba777d1c459ba0522bf98f1a064c55610230c7 Mon Sep 17 00:00:00 2001 From: neargle Date: Fri, 15 Nov 2024 19:39:53 +0800 Subject: [PATCH 4/4] chore(.gitignore): /cdk --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 7585d17..c61d511 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ cmd/cdk/cdk __debug_bin /pkg/tool/kubectl/assets/ + +/cdk