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

Remove provenance label, replace with Kind label #167

Merged
merged 2 commits into from
Sep 11, 2024
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
2 changes: 1 addition & 1 deletion internal/controller/chiaca/assemblers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var testObjMeta = metav1.ObjectMeta{
"app.kubernetes.io/instance": "testname",
"app.kubernetes.io/name": "testname",
"app.kubernetes.io/managed-by": "chia-operator",
"k8s.chia.net/provenance": "ChiaCA.testnamespace.testname",
"k8s.chia.net/kind": "ChiaCA",
},
}

Expand Down
4 changes: 1 addition & 3 deletions internal/controller/common/kube/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Copyright 2023 Chia Network Inc.
package kube

import (
"fmt"

k8schianetv1 "github.com/chia-network/chia-operator/api/v1"
"github.com/chia-network/chia-operator/internal/controller/common/consts"
corev1 "k8s.io/api/core/v1"
Expand All @@ -21,7 +19,7 @@ func GetCommonLabels(kind string, meta metav1.ObjectMeta, additionalLabels ...ma
labels["app.kubernetes.io/instance"] = meta.Name
labels["app.kubernetes.io/name"] = meta.Name
labels["app.kubernetes.io/managed-by"] = "chia-operator"
labels["k8s.chia.net/provenance"] = fmt.Sprintf("%s.%s.%s", kind, meta.Namespace, meta.Name)
labels["k8s.chia.net/kind"] = kind
return labels
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/common/kube/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGetCommonLabels(t *testing.T) {
"app.kubernetes.io/instance": "testname",
"app.kubernetes.io/name": "testname",
"app.kubernetes.io/managed-by": "chia-operator",
"k8s.chia.net/provenance": "TestKind.testnamespace.testname",
"k8s.chia.net/kind": "TestKind",
"foo": "bar",
"hello": "world",
}
Expand Down
95 changes: 88 additions & 7 deletions internal/controller/common/kube/reconcilers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,64 @@ func ReconcileDeployment(ctx context.Context, c client.Client, desired appsv1.De
} else if err != nil {
return ctrl.Result{}, fmt.Errorf("error getting existing Deployment \"%s\": %v", desired.Name, err)
} else {
// Deployment exists, so we need to update it if there are any changes
// Need to handle a case where Deployment's spec.Selector.MatchLabels changed, since the field is immutable
if !reflect.DeepEqual(current.Spec.Selector.MatchLabels, desired.Spec.Selector.MatchLabels) {
klog.Info("Recreating Deployment for new Selector labels -- selector labels are immutable")

if err := c.Delete(ctx, &current); err != nil {
if strings.Contains(err.Error(), ObjectModifiedTryAgainError) {
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}
return ctrl.Result{}, fmt.Errorf("error deleting Deployment \"%s\": %v", current.Name, err)
}

// Wait for the deployment to be deleted
for {
var tmp appsv1.Deployment
err = c.Get(ctx, types.NamespacedName{
Namespace: current.Namespace,
Name: current.Name,
}, &tmp)
if err != nil {
if client.IgnoreNotFound(err) == nil {
break
}
return ctrl.Result{}, fmt.Errorf("error waiting for Deployment to be deleted \"%s\": %v", desired.Name, err)
}
time.Sleep(2 * time.Second)
}

if err := c.Create(ctx, &desired); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating Deployment \"%s\": %v", desired.Name, err)
}

return ctrl.Result{}, nil // Exit reconciler here because we created the desired Deployment
}

// Deployment exists, so we need to update it if there are any changes.
// We'll make a copy of the current Deployment to make sure we only change mutable Deployment fields.
// Then we will compare the current and updated Deployments, and send an Update request if there was any diff.
updated := current

desiredAnnotations := CombineMaps(current.Annotations, desired.Annotations)
if !reflect.DeepEqual(current.Spec, desired.Spec) || !reflect.DeepEqual(current.Labels, desired.Labels) || !reflect.DeepEqual(current.Annotations, desiredAnnotations) {
current.Labels = desired.Labels
current.Annotations = desiredAnnotations
current.Spec = desired.Spec
if err := c.Update(ctx, &current); err != nil {
if !reflect.DeepEqual(current.Annotations, desiredAnnotations) {
updated.Annotations = desiredAnnotations
}

if !reflect.DeepEqual(current.Labels, desired.Labels) {
updated.Labels = desired.Labels
}

if !reflect.DeepEqual(current.Spec, desired.Spec) {
updated.Spec = desired.Spec
}

if !reflect.DeepEqual(current, updated) {
if err := c.Update(ctx, &updated); err != nil {
if strings.Contains(err.Error(), ObjectModifiedTryAgainError) {
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}
return ctrl.Result{}, fmt.Errorf("error updating Deployment \"%s\": %v", desired.Name, err)
return ctrl.Result{}, fmt.Errorf("error updating Deployment \"%s\": %v", updated.Name, err)
}
}
}
Expand All @@ -136,6 +183,40 @@ func ReconcileStatefulset(ctx context.Context, c client.Client, desired appsv1.S
} else if err != nil {
return ctrl.Result{}, fmt.Errorf("error getting existing StatefulSet \"%s\": %v", desired.Name, err)
} else {
// Need to handle a case where StatefulSet's spec.Selector.MatchLabels changed, since the field is immutable
if !reflect.DeepEqual(current.Spec.Selector.MatchLabels, desired.Spec.Selector.MatchLabels) {
klog.Info("Recreating StatefulSet for new Selector labels -- selector labels are immutable")

if err := c.Delete(ctx, &current); err != nil {
if strings.Contains(err.Error(), ObjectModifiedTryAgainError) {
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}
return ctrl.Result{}, fmt.Errorf("error deleting StatefulSet \"%s\": %v", current.Name, err)
}

// Wait for the statefulset to be deleted
for {
var tmp appsv1.StatefulSet
err = c.Get(ctx, types.NamespacedName{
Namespace: current.Namespace,
Name: current.Name,
}, &tmp)
if err != nil {
if client.IgnoreNotFound(err) == nil {
break
}
return ctrl.Result{}, fmt.Errorf("error waiting for StatefulSet to be deleted \"%s\": %v", desired.Name, err)
}
time.Sleep(2 * time.Second)
}

if err := c.Create(ctx, &desired); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating StatefulSet \"%s\": %v", desired.Name, err)
}

return ctrl.Result{}, nil // Exit reconciler here because we created the desired StatefulSet
}

// StatefulSet exists, so we need to update it if there are any changes.
// We'll make a copy of the current StatefulSet to make sure we only change mutable StatefulSet fields.
// Then we will compare the current and updated StatefulSets, and send an Update request if there was any diff.
Expand Down
Loading