Skip to content

Commit

Permalink
*: use 'logicalCluster' instead of 'this'
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Jan 3, 2023
1 parent fb2af5d commit 1ca8aa3
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 119 deletions.
8 changes: 4 additions & 4 deletions config/root-phase0/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func Bootstrap(ctx context.Context, kcpClient kcpclient.Interface, rootDiscovery
// set LogicalCluster to Initializing
return wait.PollImmediateUntilWithContext(ctx, time.Millisecond*100, func(ctx context.Context) (done bool, err error) {
logger := klog.FromContext(ctx).WithValues("bootstrapping", "root-phase0")
this, err := kcpClient.CoreV1alpha1().LogicalClusters().Get(ctx, corev1alpha1.LogicalClusterName, metav1.GetOptions{})
logicalCluster, err := kcpClient.CoreV1alpha1().LogicalClusters().Get(ctx, corev1alpha1.LogicalClusterName, metav1.GetOptions{})
if err != nil {
logger.Error(err, "failed to get this workspace in root")
return false, nil
}
if this.Status.Phase == corev1alpha1.LogicalClusterPhaseScheduling {
this.Status.Phase = corev1alpha1.LogicalClusterPhaseInitializing
_, err = kcpClient.CoreV1alpha1().LogicalClusters().UpdateStatus(ctx, this, metav1.UpdateOptions{})
if logicalCluster.Status.Phase == corev1alpha1.LogicalClusterPhaseScheduling {
logicalCluster.Status.Phase = corev1alpha1.LogicalClusterPhaseInitializing
_, err = kcpClient.CoreV1alpha1().LogicalClusters().UpdateStatus(ctx, logicalCluster, metav1.UpdateOptions{})
if err != nil {
logger.Error(err, "failed to update LogicalCluster root:cluster")
return false, nil
Expand Down
36 changes: 18 additions & 18 deletions pkg/admission/logicalcluster/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (o *plugin) Admit(ctx context.Context, a admission.Attributes, _ admission.
if !ok {
return fmt.Errorf("unexpected type %T", a.GetObject())
}
this := &corev1alpha1.LogicalCluster{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, this); err != nil {
logicalCluster := &corev1alpha1.LogicalCluster{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, logicalCluster); err != nil {
return fmt.Errorf("failed to convert unstructured to LogicalCluster: %w", err)
}

Expand All @@ -103,14 +103,14 @@ func (o *plugin) Admit(ctx context.Context, a admission.Attributes, _ admission.
}

// we only admit at state transition to initializing
transitioningToInitializing := old.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && this.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing
transitioningToInitializing := old.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && logicalCluster.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing
if !transitioningToInitializing {
return nil
}

this.Status.Initializers = this.Spec.Initializers
logicalCluster.Status.Initializers = logicalCluster.Spec.Initializers

return updateUnstructured(u, this)
return updateUnstructured(u, logicalCluster)
}

return nil
Expand All @@ -137,8 +137,8 @@ func (o *plugin) Validate(ctx context.Context, a admission.Attributes, _ admissi
if !ok {
return fmt.Errorf("unexpected type %T", a.GetObject())
}
this := &corev1alpha1.LogicalCluster{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, this); err != nil {
logicalCluster := &corev1alpha1.LogicalCluster{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, logicalCluster); err != nil {
return fmt.Errorf("failed to convert unstructured to LogicalCluster: %w", err)
}

Expand All @@ -152,46 +152,46 @@ func (o *plugin) Validate(ctx context.Context, a admission.Attributes, _ admissi
}

oldSpec := toSet(old.Spec.Initializers)
newSpec := toSet(this.Spec.Initializers)
newSpec := toSet(logicalCluster.Spec.Initializers)
oldStatus := toSet(old.Status.Initializers)
newStatus := toSet(this.Status.Initializers)
newStatus := toSet(logicalCluster.Status.Initializers)

if !oldSpec.Equal(newSpec) {
return admission.NewForbidden(a, fmt.Errorf("spec.initializers is immutable"))
}

transitioningToInitializing := old.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && this.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing
transitioningToInitializing := old.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && logicalCluster.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing
if transitioningToInitializing && !newSpec.Equal(newStatus) {
return admission.NewForbidden(a, fmt.Errorf("status.initializers do not equal spec.initializers"))
}

if !transitioningToInitializing && this.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing && !oldStatus.IsSuperset(newStatus) {
if !transitioningToInitializing && logicalCluster.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing && !oldStatus.IsSuperset(newStatus) {
return admission.NewForbidden(a, fmt.Errorf("status.initializers must not grow"))
}

if this.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && !oldStatus.Equal(newStatus) {
if logicalCluster.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing && !oldStatus.Equal(newStatus) {
return admission.NewForbidden(a, fmt.Errorf("status.initializers is immutable after initilization"))
}

if old.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing && this.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing {
if len(this.Status.Initializers) > 0 {
if old.Status.Phase == corev1alpha1.LogicalClusterPhaseInitializing && logicalCluster.Status.Phase != corev1alpha1.LogicalClusterPhaseInitializing {
if len(logicalCluster.Status.Initializers) > 0 {
return admission.NewForbidden(a, fmt.Errorf("status.initializers is not empty"))
}
}

if phaseOrdinal[old.Status.Phase] > phaseOrdinal[this.Status.Phase] {
return admission.NewForbidden(a, fmt.Errorf("cannot transition from %q to %q", old.Status.Phase, this.Status.Phase))
if phaseOrdinal[old.Status.Phase] > phaseOrdinal[logicalCluster.Status.Phase] {
return admission.NewForbidden(a, fmt.Errorf("cannot transition from %q to %q", old.Status.Phase, logicalCluster.Status.Phase))
}

return nil

case admission.Delete:
this, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return fmt.Errorf("LogicalCluster cannot be deleted: %w", err)
}
groups := sets.NewString(a.GetUserInfo().GetGroups()...)
if !this.Spec.DirectlyDeletable && !groups.Has(kuser.SystemPrivilegedGroup) && !groups.Has(bootstrap.SystemLogicalClusterAdmin) {
if !logicalCluster.Spec.DirectlyDeletable && !groups.Has(kuser.SystemPrivilegedGroup) && !groups.Has(bootstrap.SystemLogicalClusterAdmin) {
return admission.NewForbidden(a, fmt.Errorf("LogicalCluster cannot be deleted"))
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/admission/logicalcluster/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ func (l fakeLogicalClusterClusterLister) List(selector labels.Selector) (ret []*

func (l fakeLogicalClusterClusterLister) Cluster(cluster logicalcluster.Name) corev1alpha1listers.LogicalClusterLister {
var perCluster []*corev1alpha1.LogicalCluster
for _, this := range l {
if logicalcluster.From(this) == cluster {
perCluster = append(perCluster, this)
for _, logicalCluster := range l {
if logicalcluster.From(logicalCluster) == cluster {
perCluster = append(perCluster, logicalCluster)
}
}
return fakeLogicalClusterLister(perCluster)
Expand Down
4 changes: 2 additions & 2 deletions pkg/admission/namespacelifecycle/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ func (l *workspaceNamespaceLifecycle) Admit(ctx context.Context, a admission.Att
return apierrors.NewInternalError(err)
}

this, err := l.getLogicalCluster(clusterName)
logicalCluster, err := l.getLogicalCluster(clusterName)
// The shard hosting the workspace could be down,
// just return error from legacy namespace lifecycle admission in this case
if err != nil && !apierrors.IsNotFound(err) {
return admissionErr
}

if this.DeletionTimestamp.IsZero() {
if logicalCluster.DeletionTimestamp.IsZero() {
return admissionErr
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/admission/pathannotation/pathannotation_admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func (p *pathAnnotationPlugin) Admit(ctx context.Context, a admission.Attributes
return nil
}

this, err := p.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := p.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, fmt.Errorf("cannot get this workspace: %w", err))
}
thisPath := this.Annotations[core.LogicalClusterPathAnnotationKey]
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
if thisPath == "" {
thisPath = logicalcluster.From(this).Path().String()
thisPath = logicalcluster.From(logicalCluster).Path().String()
}

if thisPath != "" && value != thisPath {
Expand Down Expand Up @@ -142,13 +142,13 @@ func (p *pathAnnotationPlugin) Validate(ctx context.Context, a admission.Attribu

value, found := u.GetAnnotations()[core.LogicalClusterPathAnnotationKey]
if pathAnnotationResources.Has(a.GetResource().GroupResource().String()) || found {
this, err := p.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := p.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, fmt.Errorf("cannot get this workspace: %w", err))
}
thisPath := this.Annotations[core.LogicalClusterPathAnnotationKey]
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
if thisPath == "" {
thisPath = logicalcluster.From(this).Path().String()
thisPath = logicalcluster.From(logicalCluster).Path().String()
}

if value != thisPath {
Expand Down
8 changes: 4 additions & 4 deletions pkg/admission/workspace/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func (o *workspace) Admit(ctx context.Context, a admission.Attributes, _ admissi

// copy required groups from LogicalCluster to new child-Worksapce
if _, found := cw.Annotations[authorization.RequiredGroupsAnnotationKey]; !found || !isSystemPrivileged {
this, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, err)
}
if thisValue, found := this.Annotations[authorization.RequiredGroupsAnnotationKey]; found {
if thisValue, found := logicalCluster.Annotations[authorization.RequiredGroupsAnnotationKey]; found {
if cw.Annotations == nil {
cw.Annotations = map[string]string{}
}
Expand Down Expand Up @@ -200,11 +200,11 @@ func (o *workspace) Validate(ctx context.Context, a admission.Attributes, _ admi

// check that required groups match with LogicalCluster
if !isSystemPrivileged {
this, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, err)
}
expected := this.Annotations[authorization.RequiredGroupsAnnotationKey]
expected := logicalCluster.Annotations[authorization.RequiredGroupsAnnotationKey]
if cw.Annotations[authorization.RequiredGroupsAnnotationKey] != expected {
return admission.NewForbidden(a, fmt.Errorf("missing required groups annotation %s=%s", authorization.RequiredGroupsAnnotationKey, expected))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/admission/workspace/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,9 @@ func (l fakeLogicalClusterClusterLister) List(selector labels.Selector) (ret []*

func (l fakeLogicalClusterClusterLister) Cluster(cluster logicalcluster.Name) corev1alpha1listers.LogicalClusterLister {
var perCluster []*corev1alpha1.LogicalCluster
for _, this := range l {
if logicalcluster.From(this) == cluster {
perCluster = append(perCluster, this)
for _, logicalCluster := range l {
if logicalcluster.From(logicalCluster) == cluster {
perCluster = append(perCluster, logicalCluster)
}
}
return fakeLogicalClusterLister(perCluster)
Expand Down
12 changes: 6 additions & 6 deletions pkg/admission/workspacetypeexists/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ func (o *workspacetypeExists) Admit(ctx context.Context, a admission.Attributes,
return nil
}

this, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, fmt.Errorf("workspace type cannot be resolved: %w", err))
}

// if the user has not provided any type, use the default from the parent workspace
empty := tenancyv1beta1.WorkspaceTypeReference{}
if ws.Spec.Type == empty {
typeAnnotation, found := this.Annotations[tenancyv1beta1.LogicalClusterTypeAnnotationKey]
typeAnnotation, found := logicalCluster.Annotations[tenancyv1beta1.LogicalClusterTypeAnnotationKey]
if !found {
return admission.NewForbidden(a, fmt.Errorf("annotation %s on LogicalCluster must be set", tenancyv1beta1.LogicalClusterTypeAnnotationKey))
}
Expand All @@ -155,9 +155,9 @@ func (o *workspacetypeExists) Admit(ctx context.Context, a admission.Attributes,
}
}

thisPath := this.Annotations[core.LogicalClusterPathAnnotationKey]
thisPath := logicalCluster.Annotations[core.LogicalClusterPathAnnotationKey]
if thisPath == "" {
thisPath = logicalcluster.From(this).Path().String()
thisPath = logicalcluster.From(logicalCluster).Path().String()
}

cwt, err := o.resolveTypeRef(logicalcluster.NewPath(thisPath), tenancyv1alpha1.WorkspaceTypeReference{
Expand Down Expand Up @@ -292,11 +292,11 @@ func (o *workspacetypeExists) Validate(ctx context.Context, a admission.Attribut
}

// validate whether the workspace type is allowed in its parent, and the workspace type allows that parent
this, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
logicalCluster, err := o.logicalClusterLister.Cluster(clusterName).Get(corev1alpha1.LogicalClusterName)
if err != nil {
return admission.NewForbidden(a, fmt.Errorf("workspace type cannot be resolved: %w", err))
}
typeAnnotation, found := this.Annotations[tenancyv1beta1.LogicalClusterTypeAnnotationKey]
typeAnnotation, found := logicalCluster.Annotations[tenancyv1beta1.LogicalClusterTypeAnnotationKey]
if !found {
return admission.NewForbidden(a, fmt.Errorf("annotation %s on LogicalCluster must be set", tenancyv1beta1.LogicalClusterTypeAnnotationKey))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/admission/workspacetypeexists/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,9 @@ func (l fakeLogicalClusterClusterLister) List(selector labels.Selector) (ret []*

func (l fakeLogicalClusterClusterLister) Cluster(cluster logicalcluster.Name) corev1alpha1listers.LogicalClusterLister {
var perCluster []*corev1alpha1.LogicalCluster
for _, this := range l {
if logicalcluster.From(this) == cluster {
perCluster = append(perCluster, this)
for _, logicalCluster := range l {
if logicalcluster.From(logicalCluster) == cluster {
perCluster = append(perCluster, logicalCluster)
}
}
return fakeLogicalClusterLister(perCluster)
Expand Down
8 changes: 4 additions & 4 deletions pkg/authorization/requiredgroups_authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (a *requiredGroupsAuthorizer) Authorize(ctx context.Context, attr authorize

case isUser:
// get logical cluster with required group annotation
this, err := a.getLogicalCluster(cluster.Name)
logicalCluster, err := a.getLogicalCluster(cluster.Name)
if err != nil {
if errors.IsNotFound(err) {
return authorizer.DecisionNoOpinion, "logical cluster not found", nil
Expand All @@ -96,19 +96,19 @@ func (a *requiredGroupsAuthorizer) Authorize(ctx context.Context, attr authorize
}

// check required groups
value, found := this.Annotations[RequiredGroupsAnnotationKey]
value, found := logicalCluster.Annotations[RequiredGroupsAnnotationKey]
if !found {
return DelegateAuthorization("logical cluster does not require groups", a.delegate).Authorize(ctx, attr)
}
disjunctiveClauses := append(strings.Split(value, ";"), bootstrap.SystemKcpAdminGroup, bootstrap.SystemKcpWorkspaceBootstrapper)
for _, set := range disjunctiveClauses {
groups := strings.Split(set, ",")
if sets.NewString(attr.GetUser().GetGroups()...).HasAll(groups...) {
return DelegateAuthorization(fmt.Sprintf("user is member of required groups: %s", this.Annotations[RequiredGroupsAnnotationKey]), a.delegate).Authorize(ctx, attr)
return DelegateAuthorization(fmt.Sprintf("user is member of required groups: %s", logicalCluster.Annotations[RequiredGroupsAnnotationKey]), a.delegate).Authorize(ctx, attr)
}
}

return authorizer.DecisionDeny, fmt.Sprintf("user is not a member of required groups: %s", this.Annotations[RequiredGroupsAnnotationKey]), nil
return authorizer.DecisionDeny, fmt.Sprintf("user is not a member of required groups: %s", logicalCluster.Annotations[RequiredGroupsAnnotationKey]), nil
}

return authorizer.DecisionNoOpinion, WorkspaceAccessNotPermittedReason, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (c *State) DeleteWorkspace(shard string, ws *tenancyv1beta1.Workspace) {
}
}

func (c *State) UpsertLogicalCluster(shard string, this *corev1alpha1.LogicalCluster) {
clusterName := logicalcluster.From(this)
func (c *State) UpsertLogicalCluster(shard string, logicalCluster *corev1alpha1.LogicalCluster) {
clusterName := logicalcluster.From(logicalCluster)

c.lock.RLock()
got := c.clusterShards[clusterName]
Expand All @@ -148,8 +148,8 @@ func (c *State) UpsertLogicalCluster(shard string, this *corev1alpha1.LogicalClu
}
}

func (c *State) DeleteLogicalCluster(shard string, this *corev1alpha1.LogicalCluster) {
clusterName := logicalcluster.From(this)
func (c *State) DeleteLogicalCluster(shard string, logicalCluster *corev1alpha1.LogicalCluster) {
clusterName := logicalcluster.From(logicalCluster)

c.lock.RLock()
got := c.clusterShards[clusterName]
Expand Down
12 changes: 6 additions & 6 deletions pkg/proxy/index/index_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,19 @@ func (c *Controller) process(ctx context.Context, key string) error {
twInformer := corev1alpha1informers.NewLogicalClusterClusterInformer(client, resyncPeriod, nil)
twInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
this := obj.(*corev1alpha1.LogicalCluster)
c.state.UpsertLogicalCluster(shard.Name, this)
logicalCluster := obj.(*corev1alpha1.LogicalCluster)
c.state.UpsertLogicalCluster(shard.Name, logicalCluster)
},
UpdateFunc: func(old, obj interface{}) {
this := obj.(*corev1alpha1.LogicalCluster)
c.state.UpsertLogicalCluster(shard.Name, this)
logicalCluster := obj.(*corev1alpha1.LogicalCluster)
c.state.UpsertLogicalCluster(shard.Name, logicalCluster)
},
DeleteFunc: func(obj interface{}) {
if final, ok := obj.(cache.DeletedFinalStateUnknown); ok {
obj = final.Obj
}
this := obj.(*corev1alpha1.LogicalCluster)
c.state.DeleteLogicalCluster(shard.Name, this)
logicalCluster := obj.(*corev1alpha1.LogicalCluster)
c.state.DeleteLogicalCluster(shard.Name, logicalCluster)
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const (
)

type reconciler interface {
reconcile(ctx context.Context, this *corev1alpha1.LogicalCluster) (reconcileStatus, error)
reconcile(ctx context.Context, logicalCluster *corev1alpha1.LogicalCluster) (reconcileStatus, error)
}

func (c *Controller) reconcile(ctx context.Context, this *corev1alpha1.LogicalCluster) (bool, error) {
func (c *Controller) reconcile(ctx context.Context, logicalCluster *corev1alpha1.LogicalCluster) (bool, error) {
reconcilers := []reconciler{
&metaDataReconciler{},
&phaseReconciler{},
Expand All @@ -48,7 +48,7 @@ func (c *Controller) reconcile(ctx context.Context, this *corev1alpha1.LogicalCl
for _, r := range reconcilers {
var err error
var status reconcileStatus
status, err = r.reconcile(ctx, this)
status, err = r.reconcile(ctx, logicalCluster)
if err != nil {
errs = append(errs, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type urlReconciler struct {
shardExternalURL func() string
}

func (r *urlReconciler) reconcile(ctx context.Context, this *corev1alpha1.LogicalCluster) (reconcileStatus, error) {
this.Status.URL = strings.TrimSuffix(r.shardExternalURL(), "/") + logicalcluster.From(this).Path().RequestPath()
func (r *urlReconciler) reconcile(ctx context.Context, logicalCluster *corev1alpha1.LogicalCluster) (reconcileStatus, error) {
logicalCluster.Status.URL = strings.TrimSuffix(r.shardExternalURL(), "/") + logicalcluster.From(logicalCluster).Path().RequestPath()
return reconcileStatusContinue, nil
}
Loading

0 comments on commit 1ca8aa3

Please sign in to comment.