Skip to content

Commit

Permalink
fix, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Aug 17, 2023
1 parent 6c3b3c7 commit 8e008b3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/webhook/server/generic/pod/opslifecycle/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
if !utils.ControlledByPodOpsLifecycle(newPod) || operation != admissionv1.Update {
return nil
}

addReadinessGates(newPod, v1alpha1.ReadinessGatePodServiceReady)

newIdToLabelsMap, typeToNumsMap, err := podopslifecycle.PodIDAndTypesMap(newPod)
Expand Down
15 changes: 8 additions & 7 deletions pkg/webhook/server/generic/pod/opslifecycle/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod,
if !utils.ControlledByPodOpsLifecycle(newPod) {
return nil
}

expectedLabels := make(map[string]struct{})
foundLabels := make(map[string]struct{})
for label := range newPod.Labels {
Expand All @@ -41,27 +42,27 @@ func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod,
}

s := strings.Split(label, "/")
if len(s) < 2 {
if len(s) != 2 {
return fmt.Errorf("invalid label %s", label)
}
id := s[1]

if id != "" {
label := fmt.Sprintf("%s/%s", pairLabelPrefixesMap[v], id)
_, ok := newPod.Labels[label]
pairLabel := fmt.Sprintf("%s/%s", pairLabelPrefixesMap[v], id)
_, ok := newPod.Labels[pairLabel]
if !ok {
return fmt.Errorf("not found label %s", label)
return fmt.Errorf("not found label %s", pairLabel)
}
}
}

found := false
for v := range expectedLabels {
for v := range expectedLabels { // try to find the expected another label prefixes
if strings.HasPrefix(label, v) {
foundLabels[v] = struct{}{}
found = true
break
}
found = true
break
}
if found {
continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/server/generic/pod/opslifecycle/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var (

// some labels must exist together
coexistingLabelPrefixesMap = map[string]string{
v1alpha1.PodPreCheckLabelPrefix: v1alpha1.PodOperationPermissionLabelPrefix,
v1alpha1.PodOperationPermissionLabelPrefix: v1alpha1.PodPreCheckLabelPrefix,
v1alpha1.PodPreCheckedLabelPrefix: v1alpha1.PodOperationPermissionLabelPrefix,
v1alpha1.PodOperationPermissionLabelPrefix: v1alpha1.PodPreCheckedLabelPrefix,
}
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/server/generic/pod/opslifecycle/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ func TestValidating(t *testing.T) {

{
labels: map[string]string{
fmt.Sprintf("%s/%s", v1alpha1.PodPreCheckLabelPrefix, "123"): "true",
fmt.Sprintf("%s/%s", v1alpha1.PodPreCheckedLabelPrefix, "123"): "true",
fmt.Sprintf("%s/%s", v1alpha1.PodOperationPermissionLabelPrefix, "upgrade"): "1402144848",
},
},
{
labels: map[string]string{
fmt.Sprintf("%s/%s", v1alpha1.PodPreCheckLabelPrefix, "123"): "true",
fmt.Sprintf("%s/%s", v1alpha1.PodPreCheckedLabelPrefix, "123"): "true",
},
keyWords: v1alpha1.PodOperationPermissionLabelPrefix,
},
{
labels: map[string]string{
fmt.Sprintf("%s/%s", v1alpha1.PodOperationPermissionLabelPrefix, "upgrade"): "1402144848",
},
keyWords: v1alpha1.PodPreCheckLabelPrefix,
keyWords: v1alpha1.PodPreCheckedLabelPrefix,
},
{
labels: map[string]string{
Expand Down

0 comments on commit 8e008b3

Please sign in to comment.