Skip to content

Commit

Permalink
clean names
Browse files Browse the repository at this point in the history
  • Loading branch information
mjudeikis committed Oct 23, 2024
1 parent 6c63565 commit 690c6bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
76 changes: 32 additions & 44 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func New(rewriters []PathRewriter) *State {
shardClusterParentCluster: map[string]map[logicalcluster.Name]logicalcluster.Name{},
shardBaseURLs: map[string]string{},
// Experimental feature: allow mounts to be used with Workspaces
// structure: (clusterName, workspace name) -> string serialized mount objects
// structure: (shard, logical cluster, workspace name) -> string serialized mount objects
// This should be simplified once we promote this to workspace structure.
clusterWorkspaceMountAnnotation: map[string]map[logicalcluster.Name]map[string]string{},
shardClusterWorkspaceMountAnnotation: map[string]map[logicalcluster.Name]map[string]string{},

// ErrorCodeTracker is a map of logical cluster to error code when we want to return an error code
// shardCLusterWorkspaceNameErrorCode is a map of shar,logical cluster, workspace to error code when we want to return an error code
// instead of a URL.
errorCodeTracker: map[string]map[logicalcluster.Name]map[string]int{},
shardCLusterWorkspaceNameErrorCode: map[string]map[logicalcluster.Name]map[string]int{},
}
}

Expand All @@ -82,9 +82,9 @@ type State struct {
shardClusterParentCluster map[string]map[logicalcluster.Name]logicalcluster.Name // (shard name, logical cluster) -> parent logical cluster
shardBaseURLs map[string]string // shard name -> base URL
// Experimental feature: allow mounts to be used with Workspaces
clusterWorkspaceMountAnnotation map[string]map[logicalcluster.Name]map[string]string // (shard name, logical cluster, workspace name) -> mount object string
shardClusterWorkspaceMountAnnotation map[string]map[logicalcluster.Name]map[string]string // (shard name, logical cluster, workspace name) -> mount object string

errorCodeTracker map[string]map[logicalcluster.Name]map[string]int // (shard name, logical cluster, workspace name) -> error code
shardCLusterWorkspaceNameErrorCode map[string]map[logicalcluster.Name]map[string]int // (shard name, logical cluster, workspace name) -> error code
}

func (c *State) UpsertWorkspace(shard string, ws *tenancyv1alpha1.Workspace) {
Expand All @@ -94,36 +94,24 @@ func (c *State) UpsertWorkspace(shard string, ws *tenancyv1alpha1.Workspace) {

clusterName := logicalcluster.From(ws)

c.lock.RLock()
cluster := c.shardWorkspaceNameCluster[shard][clusterName][ws.Name]
mountObjString := c.clusterWorkspaceMountAnnotation[shard][clusterName][ws.Name] // experimental feature
c.lock.RUnlock()

// TODO(mjudeikis): we are allowing upsert in 2 cases:
// 1. cluster name is different
// 2. mount object string is different (updated, added, or removed)
// When we promote this to workspace structure, we should make this check smarter and better tested.
if (cluster.String() == ws.Spec.Cluster) && (ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey] != "" && mountObjString == ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey]) {
return
}

c.lock.Lock()
defer c.lock.Unlock()

// If the workspace is unavailable, we set custom error code for it. And add it to the index as normal.
// TODO(mjudeikis): Once we have one more case - move to a separate function.
if ws.Status.Phase == corev1alpha1.LogicalClusterPhaseUnavailable {
if c.errorCodeTracker[shard] == nil {
c.errorCodeTracker[shard] = map[logicalcluster.Name]map[string]int{}
if c.shardCLusterWorkspaceNameErrorCode[shard] == nil {
c.shardCLusterWorkspaceNameErrorCode[shard] = map[logicalcluster.Name]map[string]int{}
}
if c.errorCodeTracker[shard][logicalcluster.Name(ws.Spec.Cluster)] == nil {
c.errorCodeTracker[shard][logicalcluster.Name(ws.Spec.Cluster)] = map[string]int{}
if c.shardCLusterWorkspaceNameErrorCode[shard][logicalcluster.Name(ws.Spec.Cluster)] == nil {
c.shardCLusterWorkspaceNameErrorCode[shard][logicalcluster.Name(ws.Spec.Cluster)] = map[string]int{}
}
c.errorCodeTracker[shard][logicalcluster.Name(ws.Spec.Cluster)][ws.Name] = 503
// Unavailable workspaces should return 503
c.shardCLusterWorkspaceNameErrorCode[shard][logicalcluster.Name(ws.Spec.Cluster)][ws.Name] = 503
} else {
delete(c.errorCodeTracker[shard][logicalcluster.Name(ws.Spec.Cluster)], ws.Name)
if len(c.errorCodeTracker[shard][logicalcluster.Name(ws.Spec.Cluster)]) == 0 {
delete(c.errorCodeTracker[shard], logicalcluster.Name(ws.Spec.Cluster))
delete(c.shardCLusterWorkspaceNameErrorCode[shard][logicalcluster.Name(ws.Spec.Cluster)], ws.Name)
if len(c.shardCLusterWorkspaceNameErrorCode[shard][logicalcluster.Name(ws.Spec.Cluster)]) == 0 {
delete(c.shardCLusterWorkspaceNameErrorCode[shard], logicalcluster.Name(ws.Spec.Cluster))
}
}

Expand All @@ -141,14 +129,14 @@ func (c *State) UpsertWorkspace(shard string, ws *tenancyv1alpha1.Workspace) {
c.shardClusterParentCluster[shard][logicalcluster.Name(ws.Spec.Cluster)] = clusterName
}

if mountObjString := c.clusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]; mountObjString != ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey] {
if c.clusterWorkspaceMountAnnotation[shard] == nil {
c.clusterWorkspaceMountAnnotation[shard] = map[logicalcluster.Name]map[string]string{}
if mountObjString := c.shardClusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]; mountObjString != ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey] {
if c.shardClusterWorkspaceMountAnnotation[shard] == nil {
c.shardClusterWorkspaceMountAnnotation[shard] = map[logicalcluster.Name]map[string]string{}
}
if c.clusterWorkspaceMountAnnotation[shard][clusterName] == nil {
c.clusterWorkspaceMountAnnotation[shard][clusterName] = map[string]string{}
if c.shardClusterWorkspaceMountAnnotation[shard][clusterName] == nil {
c.shardClusterWorkspaceMountAnnotation[shard][clusterName] = map[string]string{}
}
c.clusterWorkspaceMountAnnotation[shard][clusterName][ws.Name] = ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey]
c.shardClusterWorkspaceMountAnnotation[shard][clusterName][ws.Name] = ws.Annotations[tenancyv1alpha1.ExperimentalWorkspaceMountAnnotationKey]
}
}

Expand All @@ -157,7 +145,7 @@ func (c *State) DeleteWorkspace(shard string, ws *tenancyv1alpha1.Workspace) {

c.lock.RLock()
_, foundCluster := c.shardWorkspaceNameCluster[shard][clusterName][ws.Name]
_, foundMount := c.clusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]
_, foundMount := c.shardClusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]
c.lock.RUnlock()

if !foundCluster && !foundMount {
Expand Down Expand Up @@ -187,15 +175,15 @@ func (c *State) DeleteWorkspace(shard string, ws *tenancyv1alpha1.Workspace) {
}
}

if _, foundMount = c.clusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]; foundMount {
delete(c.clusterWorkspaceMountAnnotation[shard][clusterName], ws.Name)
if len(c.clusterWorkspaceMountAnnotation[shard][clusterName]) == 0 {
delete(c.clusterWorkspaceMountAnnotation[shard], clusterName)
if _, foundMount = c.shardClusterWorkspaceMountAnnotation[shard][clusterName][ws.Name]; foundMount {
delete(c.shardClusterWorkspaceMountAnnotation[shard][clusterName], ws.Name)
if len(c.shardClusterWorkspaceMountAnnotation[shard][clusterName]) == 0 {
delete(c.shardClusterWorkspaceMountAnnotation[shard], clusterName)
}
}
delete(c.errorCodeTracker[shard][clusterName], ws.Name)
if len(c.errorCodeTracker[shard][clusterName]) == 0 {
delete(c.errorCodeTracker[shard], clusterName)
delete(c.shardCLusterWorkspaceNameErrorCode[shard][clusterName], ws.Name)
if len(c.shardCLusterWorkspaceNameErrorCode[shard][clusterName]) == 0 {
delete(c.shardCLusterWorkspaceNameErrorCode[shard], clusterName)
}
}

Expand Down Expand Up @@ -254,7 +242,7 @@ func (c *State) DeleteShard(shardName string) {
delete(c.shardBaseURLs, shardName)
delete(c.shardWorkspaceName, shardName)
delete(c.shardClusterParentCluster, shardName)
delete(c.errorCodeTracker, shardName)
delete(c.shardCLusterWorkspaceNameErrorCode, shardName)
}

func (c *State) Lookup(path logicalcluster.Path) (Result, bool) {
Expand Down Expand Up @@ -284,7 +272,7 @@ func (c *State) Lookup(path logicalcluster.Path) (Result, bool) {
}

// check mounts, if found return url and true
val, foundMount := c.clusterWorkspaceMountAnnotation[shard][cluster][s] // experimental feature
val, foundMount := c.shardClusterWorkspaceMountAnnotation[shard][cluster][s] // experimental feature
if foundMount {
mount, err := tenancyv1alpha1.ParseTenancyMountAnnotation(val)
if !(err != nil || mount == nil || mount.MountStatus.URL == "") {
Expand All @@ -306,7 +294,7 @@ func (c *State) Lookup(path logicalcluster.Path) (Result, bool) {
if !found {
return Result{}, false
}
ec, found := c.errorCodeTracker[shard][cluster][s]
ec, found := c.shardCLusterWorkspaceNameErrorCode[shard][cluster][s]
if found {
errorCode = ec
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (r *phaseReconciler) reconcile(ctx context.Context, workspace *tenancyv1alp
logger.Info("workspace is ready, checking conditions", "conditions", workspace.Status.Conditions)
// if workspace is ready, we check if it suppose to be ready by checking conditions.
if updateTerminalConditionsAndPhase(workspace) {
logger.Info("workspace phase changed", "status", workspace.Status)
return reconcileStatusStopAndRequeue, nil
}
}
Expand Down

0 comments on commit 690c6bd

Please sign in to comment.