/* Copyright 2021 The kcp 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 workspace import ( "context" "encoding/json" "fmt" authenticationv1 "k8s.io/api/authentication/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/klog/v2" kcpcrypto "github.com/kcp-dev/apimachinery/v2/pkg/util/crypto" corev1alpha1 "github.com/kcp-dev/sdk/apis/core/v1alpha1" tenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1" ) type metaDataReconciler struct { listShards func(selector labels.Selector) ([]*corev1alpha1.Shard, error) } func (r *metaDataReconciler) reconcile(ctx context.Context, workspace *tenancyv1alpha1.Workspace) (reconcileStatus, error) { logger := klog.FromContext(ctx).WithValues("reconciler", "metadata") if workspace.Spec.Mount != nil { return reconcileStatusContinue, nil } changed := false if workspace.Status.Phase == "" { // The storage default for .status.phase is normally applied on read. // If it is missing (a storage backend that does not decode through the // resource codec), initialize it in its own reconcile: the committer // rejects a status and a metadata change in the same pass and panics. // The label is then set on the next iteration. workspace.Status.Phase = corev1alpha1.LogicalClusterPhaseScheduling return reconcileStatusStopAndRequeue, nil } expected := string(workspace.Status.Phase) if got := workspace.Labels[tenancyv1alpha1.WorkspacePhaseLabel]; got != expected { if workspace.Labels == nil { workspace.Labels = map[string]string{} } workspace.Labels[tenancyv1alpha1.WorkspacePhaseLabel] = expected changed = true } shardName := workspace.Annotations[corev1alpha1.LogicalClusterShardAnnotationKey] switch { case shardName != "": // shard is set on new annotation, ensure the deprecated annotation matches expectedHash := kcpcrypto.Base36Sha224.StringPad(shardName)[:8] if got := workspace.Annotations[WorkspaceShardHashAnnotationKey]; got != expectedHash { if workspace.Annotations == nil { workspace.Annotations = map[string]string{} } workspace.Annotations[WorkspaceShardHashAnnotationKey] = expectedHash changed = true } default: // new annotation is not set, set it based on the deprecated annotation shardHash, hasShardHash := workspace.Annotations[WorkspaceShardHashAnnotationKey] if hasShardHash { shards, err := r.listShards(labels.Everything()) if err != nil { return reconcileStatusStopAndRequeue, fmt.Errorf("could not list shards: %w", err) } // iterate over the shards, calculate each hash and compare // to set the new annotation for _, shard := range shards { hashed := kcpcrypto.Base36Sha224.StringPad(shard.Name)[:8] if hashed != shardHash { continue } workspace.Annotations[corev1alpha1.LogicalClusterShardAnnotationKey] = shard.Name changed = true break } } } if workspace.Status.Phase == corev1alpha1.LogicalClusterPhaseReady { if value, found := workspace.Annotations[tenancyv1alpha1.ExperimentalWorkspaceOwnerAnnotationKey]; found { var info authenticationv1.UserInfo err := json.Unmarshal([]byte(value), &info) if err != nil { logger.Error(err, "failed to unmarshal workspace owner annotation", tenancyv1alpha1.ExperimentalWorkspaceOwnerAnnotationKey, value) delete(workspace.Annotations, tenancyv1alpha1.ExperimentalWorkspaceOwnerAnnotationKey) changed = true } else if userOnlyValue, err := json.Marshal(authenticationv1.UserInfo{Username: info.Username}); err != nil { // should never happen logger.Error(err, "failed to marshal user info") delete(workspace.Annotations, tenancyv1alpha1.ExperimentalWorkspaceOwnerAnnotationKey) changed = true } else if value != string(userOnlyValue) { workspace.Annotations[tenancyv1alpha1.ExperimentalWorkspaceOwnerAnnotationKey] = string(userOnlyValue) changed = true } } } if changed { // first update ObjectMeta before status return reconcileStatusStopAndRequeue, nil } return reconcileStatusContinue, nil }