From 3b15a227360580591d4ff2b134cb905ebaf83ad0 Mon Sep 17 00:00:00 2001 From: Longquan Sha Date: Tue, 11 Apr 2023 22:34:46 +0800 Subject: [PATCH 1/5] Create sonic dc daemonset --- artifacts/examples/crd-sonic.yaml | 50 +++++++++++++++++++++ pkg/apis/sonick8s/register.go | 22 ++++++++++ pkg/apis/sonick8s/v1alpha1/doc.go | 21 +++++++++ pkg/apis/sonick8s/v1alpha1/register.go | 53 ++++++++++++++++++++++ pkg/apis/sonick8s/v1alpha1/types.go | 61 ++++++++++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 artifacts/examples/crd-sonic.yaml create mode 100644 pkg/apis/sonick8s/register.go create mode 100644 pkg/apis/sonick8s/v1alpha1/doc.go create mode 100644 pkg/apis/sonick8s/v1alpha1/register.go create mode 100644 pkg/apis/sonick8s/v1alpha1/types.go diff --git a/artifacts/examples/crd-sonic.yaml b/artifacts/examples/crd-sonic.yaml new file mode 100644 index 000000000..3553bacb6 --- /dev/null +++ b/artifacts/examples/crd-sonic.yaml @@ -0,0 +1,50 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: dcdaemonsets.sonic.k8s.io + # for more information on the below annotation, please see + # https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/2337-k8s.io-group-protection/README.md + annotations: + "api-approved.kubernetes.io": "unapproved, experimental-only; please get an approval from Kubernetes API reviewers if you're trying to develop a CRD in the *.k8s.io or *.kubernetes.io groups" +spec: + group: sonic.k8s.io + versions: + - name: v1alpha1 + served: true + storage: true + schema: + # schema used for validation + openAPIV3Schema: + type: object + properties: + spec: + type: object + properties: + daemonsetVersion: + type: string + pause: + type: boolean + status: + type: object + properties: + desiredDaemonSetCount: + type: integer + currentDaemonSetCount: + type: integer + daemonsetList: + type: array + items: + type: object + properties: + daemonsetName: + type: string + daemonsetVersion: + type: string + # subresources for the custom resource + subresources: + # enables the status subresource + status: {} + names: + kind: DcDaemonSet + plural: dcdaemonsets + scope: Namespaced \ No newline at end of file diff --git a/pkg/apis/sonick8s/register.go b/pkg/apis/sonick8s/register.go new file mode 100644 index 000000000..fd5a7888d --- /dev/null +++ b/pkg/apis/sonick8s/register.go @@ -0,0 +1,22 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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 samplecontroller + +// GroupName is the group name used in this package +const ( + GroupName = "samplecontroller.k8s.io" +) diff --git a/pkg/apis/sonick8s/v1alpha1/doc.go b/pkg/apis/sonick8s/v1alpha1/doc.go new file mode 100644 index 000000000..a2f601599 --- /dev/null +++ b/pkg/apis/sonick8s/v1alpha1/doc.go @@ -0,0 +1,21 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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. +*/ + +// +k8s:deepcopy-gen=package +// +groupName=sonic.k8s.io + +// Package v1alpha1 is the v1alpha1 version of the API. +package v1alpha1 // import "k8s.io/sample-controller/pkg/apis/sonick8s/v1alpha1" diff --git a/pkg/apis/sonick8s/v1alpha1/register.go b/pkg/apis/sonick8s/v1alpha1/register.go new file mode 100644 index 000000000..cc644e54a --- /dev/null +++ b/pkg/apis/sonick8s/v1alpha1/register.go @@ -0,0 +1,53 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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 v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// SchemeGroupVersion is group version used to register these objects +var SchemeGroupVersion = schema.GroupVersion{Group: "sonic.k8s.io", Version: "v1alpha1"} + +// Kind takes an unqualified kind and returns back a Group qualified GroupKind +func Kind(kind string) schema.GroupKind { + return SchemeGroupVersion.WithKind(kind).GroupKind() +} + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} + +var ( + // SchemeBuilder initializes a scheme builder + SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) + // AddToScheme is a global function that registers this API group & version to a scheme + AddToScheme = SchemeBuilder.AddToScheme +) + +// Adds the list of known types to Scheme. +func addKnownTypes(scheme *runtime.Scheme) error { + scheme.AddKnownTypes(SchemeGroupVersion, + &DcDaemonSet{}, + &DcDaemonSetList{}, + ) + metav1.AddToGroupVersion(scheme, SchemeGroupVersion) + return nil +} diff --git a/pkg/apis/sonick8s/v1alpha1/types.go b/pkg/apis/sonick8s/v1alpha1/types.go new file mode 100644 index 000000000..5800c602b --- /dev/null +++ b/pkg/apis/sonick8s/v1alpha1/types.go @@ -0,0 +1,61 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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 v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// Foo is a specification for a Foo resource +type DcDaemonSet struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec DcDaemonSetSpec `json:"spec"` + Status DcDaemonSetStatus `json:"status"` +} + +// FooSpec is the spec for a Foo resource +type DcDaemonSetSpec struct { + DaemonSetVersion string `json:"daemonSetVersion"` + Pause bool `json:"pause"` +} + +// FooStatus is the status for a Foo resource +type DcDaemonSetStatus struct { + DesiredDaemonSetCount int `json:"desiredDaemonSetCount"` + CurrentDaemonSetCount int `json:"currentDaemonSetCount"` + DaemonsetList []DaemonSetItem `json:"daemonsetList"` +} + +type DaemonSetItem struct { + DaemonSetName string `json:"daemonSetName"` + DaemonSetVersion string `json:"daemonSetVersion"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// FooList is a list of Foo resources +type DcDaemonSetList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata"` + + Items []DcDaemonSet `json:"items"` +} From 9ab88bbf754b6750a4f4d42f0e8bab1d7af0cd83 Mon Sep 17 00:00:00 2001 From: Longquan Sha Date: Tue, 11 Apr 2023 22:36:46 +0800 Subject: [PATCH 2/5] update --- artifacts/examples/example-sonic.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 artifacts/examples/example-sonic.yaml diff --git a/artifacts/examples/example-sonic.yaml b/artifacts/examples/example-sonic.yaml new file mode 100644 index 000000000..60ddb41f3 --- /dev/null +++ b/artifacts/examples/example-sonic.yaml @@ -0,0 +1,6 @@ +apiVersion: sonic.k8s.io/v1alpha1 +kind: DcDaemonSet +metadata: + name: example-dc-daemonset +spec: + daemonsetVersion: telemetry-20220531.24 \ No newline at end of file From b85e1e0c585e9aaa347e35e959229c3ce21d4a95 Mon Sep 17 00:00:00 2001 From: Longquan Sha Date: Tue, 11 Apr 2023 23:00:15 +0800 Subject: [PATCH 3/5] update --- hack/update-codegen-sonic.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 hack/update-codegen-sonic.sh diff --git a/hack/update-codegen-sonic.sh b/hack/update-codegen-sonic.sh new file mode 100644 index 000000000..7d725ecb9 --- /dev/null +++ b/hack/update-codegen-sonic.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# Copyright 2017 The Kubernetes Authors. +# +# 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. + +set -o errexit +set -o nounset +set -o pipefail + +SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)} + +# generate the code with: +# --output-base because this script should also be able to run inside the vendor dir of +# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir +# instead of the $GOPATH directly. For normal projects this can be dropped. +"${CODEGEN_PKG}/generate-groups.sh" "deepcopy,client,informer,lister" \ + k8s.io/sample-controller/pkg/generated \ + k8s.io/sample-controller/pkg/apis \ + sonick8s:v1alpha1 \ + --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt + +# To use your own boilerplate text append: +# --go-header-file "${SCRIPT_ROOT}"/hack/custom-boilerplate.go.txt From 08232e02a22dba3040ab4ed513f4126d353958fa Mon Sep 17 00:00:00 2001 From: Longquan Sha Date: Tue, 11 Apr 2023 23:22:10 +0800 Subject: [PATCH 4/5] update --- hack/update-codegen-sonic.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/update-codegen-sonic.sh b/hack/update-codegen-sonic.sh index 7d725ecb9..10c371ce6 100644 --- a/hack/update-codegen-sonic.sh +++ b/hack/update-codegen-sonic.sh @@ -26,7 +26,7 @@ CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code- # k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir # instead of the $GOPATH directly. For normal projects this can be dropped. "${CODEGEN_PKG}/generate-groups.sh" "deepcopy,client,informer,lister" \ - k8s.io/sample-controller/pkg/generated \ + k8s.io/sample-controller/pkg/sonick8s/generated \ k8s.io/sample-controller/pkg/apis \ sonick8s:v1alpha1 \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ From efd17682983e4ef41a592100da14dfd190b5d5f0 Mon Sep 17 00:00:00 2001 From: Longquan Sha Date: Wed, 12 Apr 2023 09:26:50 +0800 Subject: [PATCH 5/5] update --- artifacts/examples/crd-sonic.yaml | 31 +++++++++++++++++++------- pkg/apis/sonick8s/v1alpha1/types.go | 34 +++++++++++++++++------------ 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/artifacts/examples/crd-sonic.yaml b/artifacts/examples/crd-sonic.yaml index 3553bacb6..587e1c853 100644 --- a/artifacts/examples/crd-sonic.yaml +++ b/artifacts/examples/crd-sonic.yaml @@ -1,13 +1,13 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: - name: dcdaemonsets.sonic.k8s.io + name: sonicdaemonsetdeployments.sonic.k8s.io # for more information on the below annotation, please see # https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/2337-k8s.io-group-protection/README.md annotations: "api-approved.kubernetes.io": "unapproved, experimental-only; please get an approval from Kubernetes API reviewers if you're trying to develop a CRD in the *.k8s.io or *.kubernetes.io groups" spec: - group: sonic.k8s.io + group: sonick8s.io versions: - name: v1alpha1 served: true @@ -20,7 +20,13 @@ spec: spec: type: object properties: - daemonsetVersion: + scopeType: + type: string + scopeValue: + type: string + daemonSetType: + type: string + daemonSetVersion: type: string pause: type: boolean @@ -31,20 +37,29 @@ spec: type: integer currentDaemonSetCount: type: integer - daemonsetList: + daemonSetList: + type: array + items: + type: object + properties: + daemonSetName: + type: string + daemonSetVersion: + type: string + updateInProgressDaemonSetList: type: array items: type: object properties: - daemonsetName: + daemonSetName: type: string - daemonsetVersion: + daemonSetVersion: type: string # subresources for the custom resource subresources: # enables the status subresource status: {} names: - kind: DcDaemonSet - plural: dcdaemonsets + kind: SonicDaemonSetDeployment + plural: sonicdaemonsetdeployments scope: Namespaced \ No newline at end of file diff --git a/pkg/apis/sonick8s/v1alpha1/types.go b/pkg/apis/sonick8s/v1alpha1/types.go index 5800c602b..f2a514b1b 100644 --- a/pkg/apis/sonick8s/v1alpha1/types.go +++ b/pkg/apis/sonick8s/v1alpha1/types.go @@ -23,26 +23,32 @@ import ( // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -// Foo is a specification for a Foo resource -type DcDaemonSet struct { +// SonicDaemonSetDeployment is a specification for a SonicDaemonSetDeployment resource +type SonicDaemonSetDeployment struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` - Spec DcDaemonSetSpec `json:"spec"` - Status DcDaemonSetStatus `json:"status"` + Spec SonicDaemonSetDeploymentSpec `json:"spec"` + Status SonicDaemonSetDeploymentStatus `json:"status"` } -// FooSpec is the spec for a Foo resource -type DcDaemonSetSpec struct { +// SonicDaemonSetDeploymentSpec is the spec for a SonicDaemonSetDeployment resource +type SonicDaemonSetDeploymentSpec struct { + // ScopeType: datacenter or region + ScopeType string `json:"scopeType"` + ScopeValue string `json:"scopeValue"` + // sonic feature such as telemetry, snmp, etc + DaemonSetType string `json:"daemonSetType"` DaemonSetVersion string `json:"daemonSetVersion"` Pause bool `json:"pause"` } -// FooStatus is the status for a Foo resource -type DcDaemonSetStatus struct { - DesiredDaemonSetCount int `json:"desiredDaemonSetCount"` - CurrentDaemonSetCount int `json:"currentDaemonSetCount"` - DaemonsetList []DaemonSetItem `json:"daemonsetList"` +// SonicDaemonSetDeploymentStatus is the status for a SonicDaemonSetDeployment resource +type SonicDaemonSetDeploymentStatus struct { + DesiredDaemonSetCount int `json:"desiredDaemonSetCount"` + CurrentDaemonSetCount int `json:"currentDaemonSetCount"` + DaemonsetList []DaemonSetItem `json:"daemonSetList"` + UpdateInProgressDaemonsetList []DaemonSetItem `json:"updateInProgressDaemonSetList"` } type DaemonSetItem struct { @@ -52,10 +58,10 @@ type DaemonSetItem struct { // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -// FooList is a list of Foo resources -type DcDaemonSetList struct { +// SonicDaemonSetDeploymentList is a list of SonicDaemonSetDeployment resources +type SonicDaemonSetDeploymentList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata"` - Items []DcDaemonSet `json:"items"` + Items []SonicDaemonSetDeployment `json:"items"` }