Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: add CRD_KIND_SPECIFIED and ETCD_SECRET_NAME env #4

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

GO_PROJECT_ROOT := github.com/erda-project/erda

REGISTRY := registry.cn-hangzhou.aliyuncs.com/dice
ifeq ($(REGISTRY_HOST),)
REGISTRY := registry.erda.cloud/erda
else
REGISTRY := $(REGISTRY_HOST)
endif

BUILD_DIR := ./build
TARGETS_DIR := dice-operator
IMAGE_PREFIX ?= $(strip )
Expand Down
4 changes: 2 additions & 2 deletions build/dice-operator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM registry.cn-hangzhou.aliyuncs.com/terminus/golang:1.16.6-stretch AS builder
FROM registry.erda.cloud/erda/golang:1.16.6-stretch AS builder

ARG GO_PROJECT_ROOT

Expand All @@ -17,7 +17,7 @@ COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/dice-operator cmd/dice-operator/main.go

FROM registry.cn-hangzhou.aliyuncs.com/terminus/alpine:3.14
FROM registry.erda.cloud/erda/alpine:3.14

ARG GO_PROJECT_ROOT

Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func buildOwnerRefs(clus *spec.DiceCluster) []metav1.OwnerReference {
blockOwnerDeletion := true
isController := true
return []metav1.OwnerReference{{
APIVersion: crd.CRDGroupVersion,
APIVersion: crd.GetCRDGroupVersion(),
Kind: crd.CRDKind,
Name: clus.Name,
UID: clus.GetUID(),
Expand Down
25 changes: 18 additions & 7 deletions pkg/cluster/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ import (
)

const (
EnableAffinity = "ENABLE_AFFINITY"
EnableSpecifiedNamespace = "ENABLE_SPECIFIED_NAMESPACE"
EnableEtcdSecret = "ENABLE_ETCD_SECRET"
EnableAffinity = "ENABLE_AFFINITY"
EnableSpecifiedNamespace = "ENABLE_SPECIFIED_NAMESPACE"
EnableEtcdSecret = "ENABLE_ETCD_SECRET"
EtcdSecretName = "ETCD_SECRET_NAME"
DefaultSecretName = "erda-etcd-client-secret"
CRDKindSpecified string = "CRD_KIND_SPECIFIED"
)

func GenName(dicesvcname string, clus *spec.DiceCluster) string {
Expand Down Expand Up @@ -230,7 +233,10 @@ func GenSAName(diceSvcName string) string {
return diceSvcName
}

return "dice-operator"
if os.Getenv(CRDKindSpecified) != "" {
return strings.ToLower(os.Getenv(CRDKindSpecified)) + "-operator"
}
return "erda-operator"
}

func EnvsFrom(clus *spec.DiceCluster) []corev1.EnvFromSource {
Expand Down Expand Up @@ -616,16 +622,21 @@ func Volumes(dicesvc *diceyml.Service) (volumes []corev1.Volume, volumeMounts []
})
}
if os.Getenv(EnableEtcdSecret) != "disable" {
name := DefaultSecretName

if os.Getenv(EtcdSecretName) != "" {
name = os.Getenv(EtcdSecretName)
}
volumes = append(volumes, corev1.Volume{
Name: "etcd-client-secret",
Name: name,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "etcd-client-secret",
SecretName: name,
},
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "etcd-client-secret",
Name: name,
ReadOnly: true,
MountPath: "/certs/",
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (c *Controller) monitor(resourceversion string, AddFunc, UpdateFunc, Delete
monitorFailCount := 0
for { // apiserver will close stream periodically? Yes
body, err := c.client.Get().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(c.cfg.Namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
VersionedParams(&metav1.ListOptions{ResourceVersion: resourceversion, Watch: true},
metav1.ParameterCodec).Stream(context.Background())
if err != nil {
Expand Down Expand Up @@ -151,9 +151,9 @@ func (c *Controller) monitor(resourceversion string, AddFunc, UpdateFunc, Delete
func (c *Controller) listDiceClusters() (*spec.DiceClusterList, error) {
result := &spec.DiceClusterList{}
req := c.client.Get().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(c.cfg.Namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
VersionedParams(&metav1.ListOptions{}, metav1.ParameterCodec)
r, err := req.DoRaw(context.Background())
if err != nil {
Expand Down
66 changes: 54 additions & 12 deletions pkg/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ package crd

import (
"context"
"fmt"
"os"
"strings"

apiextensionv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextension "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand All @@ -24,13 +27,12 @@ import (
)

const (
CRDKind string = "Dice"
CRDSingular string = "dice"
CRDPlural string = "dices"
CRDGroup string = "dice.terminus.io"
CRDVersion string = "v1beta1"
CRDFullname string = CRDPlural + "." + CRDGroup
CRDGroupVersion string = CRDGroup + "/" + CRDVersion
CRDKind string = "Erda"
CRDSingular string = "erda"
CRDPlural string = "erdas"
CRDGroup string = "erda.terminus.io"
CRDVersion string = "v1beta1"
CRDKindSpecified string = "CRD_KIND_SPECIFIED"
)

// CreateCRD create 'dice' crd if not exists yet
Expand All @@ -39,20 +41,21 @@ func CreateCRD(config *rest.Config) error {
if err != nil {
return err
}

crd := apiextensionv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: CRDFullname},
ObjectMeta: metav1.ObjectMeta{Name: GetCRDFullName()},
Spec: apiextensionv1beta1.CustomResourceDefinitionSpec{
Group: CRDGroup,
Group: GetCRDGroup(),
Versions: []apiextensionv1beta1.CustomResourceDefinitionVersion{{
Name: CRDVersion,
Served: true,
Storage: true,
}},
Scope: apiextensionv1beta1.NamespaceScoped,
Names: apiextensionv1beta1.CustomResourceDefinitionNames{
Plural: CRDPlural,
Singular: CRDSingular,
Kind: CRDKind,
Plural: GetCRDPlural(),
Singular: GetCRDSingular(),
Kind: GetCRDKind(),
},
AdditionalPrinterColumns: []apiextensionv1beta1.CustomResourceColumnDefinition{
{
Expand All @@ -79,3 +82,42 @@ func CreateCRD(config *rest.Config) error {
}
return err
}

func GetCRDPlural() string {
if os.Getenv(CRDKindSpecified) != "" {
return fmt.Sprintf("%ss", strings.ToLower(os.Getenv(CRDKindSpecified)))
}
return CRDPlural
}

func GetCRDSingular() string {
if os.Getenv(CRDKindSpecified) != "" {
return fmt.Sprintf("%s", strings.ToLower(os.Getenv(CRDKindSpecified)))
}
return CRDSingular
}

func GetCRDKind() string {
if os.Getenv(CRDKindSpecified) != "" {
return os.Getenv(CRDKindSpecified)
}
return CRDKind
}

func GetCRDGroup() string {
if os.Getenv(CRDKindSpecified) != "" {
return fmt.Sprintf("%s.terminus.io", strings.ToLower(os.Getenv(CRDKindSpecified)))
}
return CRDGroup
}

func GetCRDFullName() string {
plural := GetCRDPlural()
group := GetCRDGroup()
return fmt.Sprintf("%s.%s", plural, group)
}

func GetCRDGroupVersion() string {
group := GetCRDGroup()
return fmt.Sprintf("%s/%s", group, CRDVersion)
}
16 changes: 8 additions & 8 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func UpdateComponentStatus(client rest.Interface, namespace, name string, status

func auxUpdateStatus(client rest.Interface, namespace, name string, f func(old spec.ClusterStatus) spec.ClusterStatus) error {
raw, err := client.Get().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
Name(name).
DoRaw(context.Background())
if err != nil {
Expand All @@ -86,9 +86,9 @@ func auxUpdateStatus(client rest.Interface, namespace, name string, f func(old s
return err
}
r, err := client.Put().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
Name(name).
SubResource("status").
Body(after).
Expand All @@ -102,9 +102,9 @@ func auxUpdateStatus(client rest.Interface, namespace, name string, f func(old s

func RevertResetStatus(client rest.Interface, namespace, name string) error {
raw, err := client.Get().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
Name(name).
DoRaw(context.Background())
if err != nil {
Expand All @@ -122,9 +122,9 @@ func RevertResetStatus(client rest.Interface, namespace, name string) error {
return err
}
r, err := client.Put().
Prefix("apis", crd.CRDGroupVersion).
Prefix("apis", crd.GetCRDGroupVersion()).
Namespace(namespace).
Resource(crd.CRDPlural).
Resource(crd.GetCRDPlural()).
Name(name).
Body(after).
DoRaw(context.Background())
Expand Down