-
Notifications
You must be signed in to change notification settings - Fork 876
feat: Implement config pod status #3544
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
Merged
ritazh
merged 30 commits into
open-policy-agent:master
from
abhipatnala:add_status_to_config
Oct 11, 2024
Merged
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
dfe3878
add config pod status crd
14d80aa
generate manifests
369004c
implement config status controller wip
9c27e83
implement config pod status logic
290b46e
remove make comment from api docs
2d6ccba
fix go lint errors
b149d3f
fix helm charts
bc5ba07
fix double import
8806502
add or delete pod status at the end
a2f6e9e
pass request.name as key when config is deleted
9cf8fe5
update config controller to pass request.NameSpace.Name
00c3e41
wrap fake client with mutex logic to resolve data race issues
842b26a
use wrapped fake client in failing test
9d05fc2
fix typo in retry count
5f220bb
fix config by pod status not updating issue
0f431eb
Address review comments
79c048b
fix lint errors
9c5b5d7
add unit tests for config pod status types
3a4e7b2
write upsert error to status obj
1d44b5f
update kubebuilder tag in config status controller
f371fb3
pass namespace to reconcile request of config controller
45c2da7
address review comments
4dae25f
use namespace stored in config pod status
b326059
store namespace in configpod status name
b75a3ea
address review comments
8201cff
wrap fake client with mutex in unit tests
958be2c
fix expansion template status to use self only and gate all status co…
a860f6c
remove gating in mutator status controller
3600f97
Merge branch 'master' into add_status_to_config
ritazh 20cb2ca
Merge branch 'master' into add_status_to_config
ritazh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package v1beta1 | ||
|
|
||
| import ( | ||
| "github.com/open-policy-agent/gatekeeper/v3/pkg/operations" | ||
| "github.com/open-policy-agent/gatekeeper/v3/pkg/util" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| ) | ||
|
|
||
| // ConfigPodStatusStatus defines the observed state of ConfigPodStatus. | ||
|
|
||
| // +kubebuilder:object:generate=true | ||
|
|
||
| type ConfigPodStatusStatus struct { | ||
| ID string `json:"id,omitempty"` | ||
| ConfigUID types.UID `json:"configUID,omitempty"` | ||
| Operations []string `json:"operations,omitempty"` | ||
| ObservedGeneration int64 `json:"observedGeneration,omitempty"` | ||
| Errors []*ConfigError `json:"errors,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:generate=true | ||
|
|
||
| type ConfigError struct { | ||
| Type string `json:"type,omitempty"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| // ConfigPodStatus is the Schema for the configpodstatuses API. | ||
|
|
||
| // +kubebuilder:object:root=true | ||
| // +kubebuilder:resource:scope=Namespaced | ||
|
|
||
| type ConfigPodStatus struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
|
||
| Status ConfigPodStatusStatus `json:"status,omitempty"` | ||
| } | ||
|
|
||
| // ConfigPodStatusList contains a list of ConfigPodStatus. | ||
|
|
||
| // +kubebuilder:object:root=true | ||
| type ConfigPodStatusList struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ListMeta `json:"metadata,omitempty"` | ||
| Items []ConfigPodStatus `json:"items"` | ||
| } | ||
|
|
||
| func init() { | ||
| SchemeBuilder.Register(&ConfigPodStatus{}, &ConfigPodStatusList{}) | ||
| } | ||
|
|
||
| // NewConfigStatusForPod returns an config status object | ||
| // that has been initialized with the bare minimum of fields to make it functional | ||
| // with the config status controller. | ||
| func NewConfigStatusForPod(pod *corev1.Pod, configNamespace string, configName string, scheme *runtime.Scheme) (*ConfigPodStatus, error) { | ||
| obj := &ConfigPodStatus{} | ||
| name, err := KeyForConfig(pod.Name, configNamespace, configName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| obj.SetName(name) | ||
| obj.SetNamespace(util.GetNamespace()) | ||
| obj.Status.ID = pod.Name | ||
| obj.Status.Operations = operations.AssignedStringList() | ||
| obj.SetLabels(map[string]string{ | ||
| ConfigNameLabel: configName, | ||
| PodLabel: pod.Name, | ||
| }) | ||
|
|
||
| if err := controllerutil.SetOwnerReference(pod, obj, scheme); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return obj, nil | ||
| } | ||
|
|
||
| // KeyForConfig returns a unique status object name given the Pod ID and | ||
| // a config object. | ||
| // The object name must satisfy RFC 1123 Label Names spec | ||
| // (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/) | ||
| // and Kubernetes validation rules for object names. | ||
| // | ||
| // It's possible that dash packing/unpacking would result in a name | ||
| // that exceeds the maximum length allowed, but for Config resources, | ||
| // the configName should always be "config", and namespace would be "gatekeeper-system", | ||
| // so this validation will hold. | ||
| func KeyForConfig(id string, configNamespace string, configName string) (string, error) { | ||
| return DashPacker(id, configNamespace, configName) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package v1beta1_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1" | ||
| "github.com/open-policy-agent/gatekeeper/v3/pkg/fakes" | ||
| "github.com/open-policy-agent/gatekeeper/v3/pkg/operations" | ||
| "github.com/open-policy-agent/gatekeeper/v3/test/testutils" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| ) | ||
|
|
||
| func TestNewConfigStatusForPod(t *testing.T) { | ||
| const podName = "some-gk-pod" | ||
| const podNS = "a-gk-namespace" | ||
| const configName = "a-config" | ||
| const configNameSpace = "a-gk-ns" | ||
|
|
||
| testutils.Setenv(t, "POD_NAMESPACE", podNS) | ||
|
|
||
| scheme := runtime.NewScheme() | ||
| err := v1beta1.AddToScheme(scheme) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = corev1.AddToScheme(scheme) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| pod := fakes.Pod( | ||
| fakes.WithNamespace(podNS), | ||
| fakes.WithName(podName), | ||
| ) | ||
|
|
||
| expectedStatus := &v1beta1.ConfigPodStatus{} | ||
| expectedStatus.SetName("some--gk--pod-a--gk--ns-a--config") | ||
| expectedStatus.SetNamespace(podNS) | ||
| expectedStatus.Status.ID = podName | ||
| expectedStatus.Status.Operations = operations.AssignedStringList() | ||
| expectedStatus.SetLabels(map[string]string{ | ||
| v1beta1.ConfigNameLabel: configName, | ||
| v1beta1.PodLabel: podName, | ||
| }) | ||
|
|
||
| err = controllerutil.SetOwnerReference(pod, expectedStatus, scheme) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| status, err := v1beta1.NewConfigStatusForPod(pod, configNameSpace, configName, scheme) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if diff := cmp.Diff(expectedStatus, status); diff != "" { | ||
| t.Fatal(diff) | ||
| } | ||
| n, err := v1beta1.KeyForConfig(podName, configNameSpace, configName) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if status.Name != n { | ||
| t.Fatal("got status.Name != n, want equal") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.