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

register podopslifecycle webhook #26

Merged
merged 10 commits into from
Aug 17, 2023
Merged

register podopslifecycle webhook #26

merged 10 commits into from
Aug 17, 2023

Conversation

shaofan-hs
Copy link
Collaborator

@shaofan-hs shaofan-hs commented Aug 15, 2023

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

  • N
  • Y

re #11 #28 #21

2. What is the scope of this PR (e.g. component or file name):

3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Other

4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

  • N
  • Y

5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

  • Unit test
  • Integration test
  • Benchmark (add benchmark stats below)
  • Manual test (add detailed scripts or steps below)
  • Other

6. Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

apis/apps/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/webhook/server/generic/pod/opslifecycle/mutating.go Outdated Show resolved Hide resolved
pkg/webhook/server/generic/pod/opslifecycle/webhook.go Outdated Show resolved Hide resolved
Comment on lines +31 to +83
func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error {
if !utils.ControlledByPodOpsLifecycle(newPod) {
return nil
}

expectedLabels := make(map[string]struct{})
foundLabels := make(map[string]struct{})
for label := range newPod.Labels {
for _, v := range pairLabelPrefixesMap { // labels must exist together and have the same id
if !strings.HasPrefix(label, v) {
continue
}

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

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

found := false
for v := range expectedLabels { // try to find the expected another label prefixes
if strings.HasPrefix(label, v) {
foundLabels[v] = struct{}{}
found = true
break
}
}
if found {
continue
}

for _, v := range coexistingLabelPrefixesMap { // labels must exist together
if !strings.HasPrefix(label, v) {
continue
}
expectedLabels[coexistingLabelPrefixesMap[v]] = struct{}{}
}
}

if len(expectedLabels) != len(foundLabels) {
return fmt.Errorf("not found the expected label prefixes: %v", expectedLabels)
}
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func (lc *OpsLifecycle) Validating(ctx context.Context, c client.Client, oldPod, newPod *corev1.Pod, operation admissionv1.Operation) error {
	if !utils.ControlledByPodOpsLifecycle(newPod) {
		return nil
	}
	allPrefix := sets.NewString()
	for label := range newPod.Labels {

		s := strings.Split(label, "/")
		if len(s) != 2 {
			return fmt.Errorf("invalid label %s", label)
		}
		prefix, id := s[0], s[1]
		allPrefix.Insert(prefix)
		for _, v := range pairLabelPrefixesMap { // labels must exist together and have the same id
			if prefix != v {
				continue
			}
			if id != "" {
				pairLabel := fmt.Sprintf("%s/%s", pairLabelPrefixesMap[v], id)
				_, ok := newPod.Labels[pairLabel]
				if !ok {
					return fmt.Errorf("not found label %s", pairLabel)
				}
			}
		}
	}
	for prefix := range allPrefix {
		pair := coexistingLabelPrefixesMap[prefix]
		if pair != "" && !allPrefix.Has(pair) {
			return fmt.Errorf("not found the expected label prefixes: %v", pair)
		}
	}
	return nil
}

@Eikykun Eikykun merged commit becbf99 into main Aug 17, 2023
5 checks passed
@Eikykun Eikykun deleted the podopslifecycle3 branch August 17, 2023 08:17
@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants