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

Add missing labels and annotations to superuserSecret #608

Merged
merged 2 commits into from
Jan 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti
## unreleased

* [FEATURE] [#601](https://github.com/k8ssandra/cass-operator/pull/601) Add additionalAnnotations field to CR so that all resources created by the operator can be annotated.
* [BUGFIX] [#607](https://github.com/k8ssandra/cass-operator/issues/607) Add missing additional labels and annotations to the superuserSecret.

## v1.18.2

Expand Down
2 changes: 1 addition & 1 deletion apis/cassandra/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/config/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/control/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/oplabels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const (
)

func AddOperatorLabels(m map[string]string, dc *api.CassandraDatacenter) {
if m == nil {
m = make(map[string]string)
}
m[ManagedByLabel] = ManagedByLabelValue
m[NameLabel] = NameLabelValue
m[VersionLabel] = dc.Spec.ServerVersion
Expand Down
7 changes: 2 additions & 5 deletions pkg/reconciliation/construct_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ func newServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Servi
}

service.Spec.Ports = ports
anns := make(map[string]string)
oplabels.AddOperatorAnnotations(anns, dc)
service.ObjectMeta.Annotations = anns

addAdditionalOptions(service, &dc.Spec.AdditionalServiceConfig.DatacenterService)

Expand Down Expand Up @@ -143,7 +140,7 @@ func newSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.S
func newAdditionalSeedServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Service {
labels := dc.GetDatacenterLabels()
oplabels.AddOperatorLabels(labels, dc)
anns := dc.GetAnnotations()
anns := make(map[string]string)
oplabels.AddOperatorAnnotations(anns, dc)
var service corev1.Service
service.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName()
Expand All @@ -169,7 +166,7 @@ func newEndpointsForAdditionalSeeds(dc *api.CassandraDatacenter) (*corev1.Endpoi
endpoints.ObjectMeta.Name = dc.GetAdditionalSeedsServiceName()
endpoints.ObjectMeta.Namespace = dc.Namespace
endpoints.ObjectMeta.Labels = labels
anns := dc.GetAnnotations()
anns := make(map[string]string)
oplabels.AddOperatorAnnotations(anns, dc)
endpoints.ObjectMeta.Annotations = anns

Expand Down
17 changes: 0 additions & 17 deletions pkg/reconciliation/reconcile_racks.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,23 +457,6 @@ func (rc *ReconciliationContext) CheckRackLabels() result.ReconcileResult {
rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource,
"Update rack annotations for StatefulSet %s", statefulSet.Name)
}

ptsAnns := statefulSet.Spec.Template.GetAnnotations()
oplabels.AddOperatorAnnotations(ptsAnns, rc.Datacenter)
if !reflect.DeepEqual(ptsAnns, statefulSet.GetAnnotations()) {
rc.ReqLogger.Info("Updating annotations",
"statefulSet", statefulSet,
"current", ptsAnns,
"desired", updatedLabels)
statefulSet.Spec.Template.SetAnnotations(ptsAnns)

if err := rc.Client.Patch(rc.Ctx, statefulSet, patch); err != nil {
return result.Error(err)
}

rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeNormal, events.LabeledRackResource,
"Update pod template spec rack annotations for StatefulSet %s", statefulSet.Name)
}
}

return result.Continue()
Expand Down
37 changes: 21 additions & 16 deletions pkg/reconciliation/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ func buildDefaultSuperuserSecret(dc *api.CassandraDatacenter) (*corev1.Secret, e
var secret *corev1.Secret = nil

if dc.ShouldGenerateSuperuserSecret() {
labels := make(map[string]string)
oplabels.AddOperatorLabels(labels, dc)
anns := make(map[string]string)
oplabels.AddOperatorAnnotations(anns, dc)

secretNamespacedName := dc.GetSuperuserSecretNamespacedName()
secret = &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Expand All @@ -69,10 +64,12 @@ func buildDefaultSuperuserSecret(dc *api.CassandraDatacenter) (*corev1.Secret, e
ObjectMeta: metav1.ObjectMeta{
Name: secretNamespacedName.Name,
Namespace: secretNamespacedName.Namespace,
Labels: labels,
Annotations: anns,
Labels: dc.GetDatacenterLabels(),
Annotations: make(map[string]string),
},
}
oplabels.AddOperatorLabels(secret.Labels, dc)
oplabels.AddOperatorAnnotations(secret.Annotations, dc)
username := api.CleanupForKubernetes(dc.Spec.ClusterName) + "-superuser"
password, err := generateUtf8Password()
if err != nil {
Expand Down Expand Up @@ -152,10 +149,16 @@ func (rc *ReconciliationContext) createInternodeCACredential() (*corev1.Secret,
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: rc.keystoreCASecret().Name,
Namespace: rc.keystoreCASecret().Namespace,
Name: rc.keystoreCASecret().Name,
Namespace: rc.keystoreCASecret().Namespace,
Labels: rc.Datacenter.GetDatacenterLabels(),
Annotations: make(map[string]string),
},
}

oplabels.AddOperatorLabels(secret.Labels, rc.Datacenter)
oplabels.AddOperatorAnnotations(secret.Annotations, rc.Datacenter)

if keypem, certpem, err := utils.GetNewCAandKey(fmt.Sprintf("%s-ca-keystore", rc.Datacenter.Name), rc.Datacenter.Namespace); err == nil {
secret.Data = map[string][]byte{
"key": []byte(keypem),
Expand All @@ -168,30 +171,32 @@ func (rc *ReconciliationContext) createInternodeCACredential() (*corev1.Secret,
}

func (rc *ReconciliationContext) createCABootstrappingSecret(jksBlob []byte) error {
_, err := rc.retrieveSecret(types.NamespacedName{
if _, err := rc.retrieveSecret(types.NamespacedName{
Name: fmt.Sprintf("%s-keystore", rc.Datacenter.Name),
Namespace: rc.Datacenter.Namespace,
})

if err == nil { // This secret already exists, nothing to do
}); err == nil {
return nil
}

secret := &corev1.Secret{

TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-keystore", rc.Datacenter.Name),
Namespace: rc.Datacenter.Namespace,
Name: fmt.Sprintf("%s-keystore", rc.Datacenter.Name),
Namespace: rc.Datacenter.Namespace,
Labels: make(map[string]string),
Annotations: make(map[string]string),
},
}
secret.Data = map[string][]byte{
"node-keystore.jks": jksBlob,
}

oplabels.AddOperatorLabels(secret.Labels, rc.Datacenter)
oplabels.AddOperatorAnnotations(secret.Annotations, rc.Datacenter)

return rc.Client.Create(rc.Ctx, secret)
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/reconciliation/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_buildDefaultSuperuserSecret(t *testing.T) {
t.Run("test default superuser secret is created", func(t *testing.T) {
dc := &api.CassandraDatacenter{
ObjectMeta: metav1.ObjectMeta{
Name: "exampleDC",
Name: "exampledc",
Namespace: "examplens",
},
Spec: api.CassandraDatacenterSpec{
Expand Down Expand Up @@ -52,6 +52,8 @@ func Test_buildDefaultSuperuserSecret(t *testing.T) {
}

expectedSecretLabels := map[string]string{
api.ClusterLabel: "exampleCluster",
api.DatacenterLabel: "exampledc",
oplabels.InstanceLabel: "cassandra-exampleCluster",
oplabels.ManagedByLabel: oplabels.ManagedByLabelValue,
oplabels.NameLabel: oplabels.NameLabelValue,
Expand Down
Loading