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

Add labels and annotations field to SubNamespace resource #19

Merged
merged 28 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eccf069
Add labels and annotations field to SubNamespace resource
bells17 Sep 23, 2021
59b8588
Fix
bells17 Oct 5, 2021
71b1a32
Add comments
bells17 Oct 5, 2021
cff5a5a
Fix codes
bells17 Oct 12, 2021
05ae0c2
Merge branch 'main' into add-labels-and-annotations-field2
bells17 Oct 12, 2021
15fd9ee
Fix code
bells17 Oct 19, 2021
6b2cef1
Update chart version
bells17 Oct 19, 2021
8fac21a
Merge branch 'main' into add-labels-and-annotations-field2
bells17 Oct 19, 2021
edc2192
Add config params for keys of SubNamespace spec. labels/spec.annotati…
bells17 Nov 1, 2021
08dff57
Merge branch 'main' into add-labels-and-annotations-field2
bells17 Nov 1, 2021
c95b17e
Update chart version
bells17 Nov 1, 2021
bc02226
Fix configmap.yaml template
bells17 Nov 1, 2021
15d51ad
Update charts/accurate/MIGRATION.md
bells17 Nov 2, 2021
89517fb
Update charts/accurate/values.yaml
bells17 Nov 2, 2021
cc73a0b
Add --annotations and --labels option to kubectl-accurate sub create …
bells17 Nov 8, 2021
55c333e
Remove unused code
bells17 Nov 15, 2021
0e5f2ed
Merge branch 'main' into add-labels-and-annotations-field2
bells17 Nov 16, 2021
b737d26
Modify to propagate SubNamespace labels/annotations to descendant nam…
bells17 Nov 17, 2021
3a004b9
Fix
bells17 Nov 17, 2021
47876b5
Add testcases
bells17 Nov 17, 2021
4e5e4a0
Add docs for SubNamespace labels/annotations
bells17 Nov 17, 2021
f8f1134
Update docs
bells17 Nov 17, 2021
1302f9f
Remove SubNamespace DeleteFunc from NamespaceReconciler
bells17 Nov 18, 2021
d812f76
Update docs/config.md
bells17 Dec 1, 2021
5b89d8f
Add example values to descriptions
bells17 Dec 1, 2021
a2ba084
Add check that the namespace is a sub-namespace
bells17 Dec 1, 2021
5fa63b5
Add validations for spec.labels and spec.annotations fields
bells17 Dec 1, 2021
75d104c
Modify to skip when apierrors.IsNotFound(err) is true
bells17 Dec 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/v1/subnamespace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,28 @@ const (
SubNamespaceConflict = SubNamespaceStatus("conflict")
)

// SubNamespaceSpec defines the desired state of SubNamespace
type SubNamespaceSpec struct {
ymmt2005 marked this conversation as resolved.
Show resolved Hide resolved
// Labels is the labels for be propagated to the sub-namespace.
ymmt2005 marked this conversation as resolved.
Show resolved Hide resolved
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Annotations is the annotations for be propagated to the sub-namespace.
ymmt2005 marked this conversation as resolved.
Show resolved Hide resolved
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

//+kubebuilder:object:root=true

// SubNamespace is the Schema for the subnamespaces API
type SubNamespace struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Spec is the spec of SubNamespace.
// +optional
Spec SubNamespaceSpec `json:"spec,omitempty"`

// Status is the status of SubNamespace.
// +optional
Status SubNamespaceStatus `json:"status,omitempty"`
Expand Down
30 changes: 30 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charts/accurate/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
version: 0.1.1

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
15 changes: 15 additions & 0 deletions charts/accurate/crds/accurate.cybozu.com_subnamespaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ spec:
type: string
metadata:
type: object
spec:
description: Spec is the spec of SubNamespace.
properties:
annotations:
additionalProperties:
type: string
description: Annotations is the annotations for be propagated to the
sub-namespace.
type: object
labels:
additionalProperties:
type: string
description: Labels is the labels for be propagated to the sub-namespace.
type: object
type: object
status:
description: Status is the status of SubNamespace.
enum:
Expand Down
4 changes: 3 additions & 1 deletion cmd/accurate-controller/sub/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func subMain(ns, addr string, port int) error {

// SubNamespace reconciler & webhook
if err = (&controllers.SubNamespaceReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
LabelKeys: cfg.LabelKeys,
AnnotationKeys: cfg.AnnotationKeys,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create SubNamespace controller: %w", err)
}
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/accurate.cybozu.com_subnamespaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ spec:
type: string
metadata:
type: object
spec:
description: Spec is the spec of SubNamespace.
properties:
annotations:
additionalProperties:
type: string
description: Annotations is the annotations for be propagated to the
sub-namespace.
type: object
labels:
additionalProperties:
type: string
description: Labels is the labels for be propagated to the sub-namespace.
type: object
type: object
status:
description: Status is the status of SubNamespace.
enum:
Expand Down
29 changes: 13 additions & 16 deletions controllers/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,11 @@ func (r *NamespaceReconciler) propagateMeta(ctx context.Context, ns, parent *cor
}

func (r *NamespaceReconciler) matchLabelKey(key string) bool {
for _, l := range r.LabelKeys {
// The glob pattern has been verified to be in the valid format when reading the config file.
if ok, _ := path.Match(l, key); ok {
return true
}
}

return false
return matchKey(key, r.LabelKeys)
}

func (r *NamespaceReconciler) matchAnnotationKey(key string) bool {
for _, a := range r.AnnotationKeys {
// The glob pattern has been verified to be in the valid format when reading the config file.
if ok, _ := path.Match(a, key); ok {
return true
}
}

return false
return matchKey(key, r.AnnotationKeys)
}

func (r *NamespaceReconciler) propagateResource(ctx context.Context, res *unstructured.Unstructured, parent, ns string) error {
Expand Down Expand Up @@ -345,3 +331,14 @@ func (r *NamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&corev1.Namespace{}).
Complete(r)
}

func matchKey(key string, list []string) bool {
for _, l := range list {
// The glob pattern has been verified to be in the valid format when reading the config file.
if ok, _ := path.Match(l, key); ok {
return true
}
}

return false
}
26 changes: 26 additions & 0 deletions controllers/subnamespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
// SubNamespaceReconciler reconciles a SubNamespace object
type SubNamespaceReconciler struct {
client.Client
LabelKeys []string
AnnotationKeys []string
}

//+kubebuilder:rbac:groups=accurate.cybozu.com,resources=subnamespaces,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -113,6 +115,22 @@ func (r *SubNamespaceReconciler) reconcileNS(ctx context.Context, sn *accuratev1

if ns.Labels[constants.LabelParent] == sn.Namespace {
sn.Status = accuratev1.SubNamespaceOK
for k, v := range sn.Spec.Labels {
if ok := r.matchLabelKey(k); ok {
ns.Labels[k] = v
}
}
if ns.Annotations == nil {
ymmt2005 marked this conversation as resolved.
Show resolved Hide resolved
ns.Annotations = make(map[string]string)
}
for k, v := range sn.Spec.Annotations {
if ok := r.matchAnnotationKey(k); ok {
ns.Annotations[k] = v
}
}
if err := r.Update(ctx, ns); err != nil {
return err
}
ymmt2005 marked this conversation as resolved.
Show resolved Hide resolved
} else {
logger.Info("a conflicting namespace already exists")
sn.Status = accuratev1.SubNamespaceConflict
Expand All @@ -121,6 +139,14 @@ func (r *SubNamespaceReconciler) reconcileNS(ctx context.Context, sn *accuratev1
return r.Update(ctx, sn)
}

func (r *SubNamespaceReconciler) matchLabelKey(key string) bool {
return matchKey(key, r.LabelKeys)
}

func (r *SubNamespaceReconciler) matchAnnotationKey(key string) bool {
return matchKey(key, r.AnnotationKeys)
}

// SetupWithManager sets up the controller with the Manager.
func (r *SubNamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
nsHandler := func(o client.Object, q workqueue.RateLimitingInterface) {
Expand Down
17 changes: 16 additions & 1 deletion controllers/subnamespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var _ = Describe("SubNamespace controller", func() {
Expect(err).ToNot(HaveOccurred())

snr := &SubNamespaceReconciler{
Client: mgr.GetClient(),
Client: mgr.GetClient(),
LabelKeys: []string{"exists"},
AnnotationKeys: []string{"exists"},
}
err = snr.SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -60,6 +62,14 @@ var _ = Describe("SubNamespace controller", func() {
sn.Namespace = "test1"
sn.Name = "test1-sub1"
sn.Finalizers = []string{constants.Finalizer}
sn.Spec.Labels = map[string]string{
"exists": "true",
"not-exists": "true",
}
sn.Spec.Annotations = map[string]string{
"exists": "true",
"not-exists": "true",
}
err = k8sClient.Create(ctx, sn)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -72,6 +82,11 @@ var _ = Describe("SubNamespace controller", func() {
Expect(sub1.Labels).To(HaveKeyWithValue(constants.LabelCreatedBy, "accurate"))
Expect(sub1.Labels).To(HaveKeyWithValue(constants.LabelParent, "test1"))

Expect(sub1.Labels).To(HaveKeyWithValue("exists", "true"))
Expect(sub1.Annotations).To(HaveKeyWithValue("exists", "true"))
Expect(sub1.Labels).NotTo(HaveKeyWithValue("not-exists", "true"))
Expect(sub1.Annotations).NotTo(HaveKeyWithValue("not-exists", "true"))

Eventually(func() accuratev1.SubNamespaceStatus {
sn = &accuratev1.SubNamespace{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test1", Name: "test1-sub1"}, sn)
Expand Down
13 changes: 13 additions & 0 deletions docs/crd_subnamespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Sub Resources

* [SubNamespaceList](#subnamespacelist)
* [SubNamespaceSpec](#subnamespacespec)

#### SubNamespace

Expand All @@ -14,6 +15,7 @@ SubNamespace is the Schema for the subnamespaces API
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| metadata | | metav1.ObjectMeta | false |
| spec | Spec is the spec of SubNamespace. | [SubNamespaceSpec](#subnamespacespec) | false |
| status | Status is the status of SubNamespace. | SubNamespaceStatus | false |

[Back to Custom Resources](#custom-resources)
Expand All @@ -28,3 +30,14 @@ SubNamespaceList contains a list of SubNamespace
| items | | [][SubNamespace](#subnamespace) | true |

[Back to Custom Resources](#custom-resources)

#### SubNamespaceSpec

SubNamespaceSpec defines the desired state of SubNamespace

| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| labels | Labels is the labels for be propagated to the sub-namespace. | map[string]string | false |
| annotations | Annotations is the annotations for be propagated to the sub-namespace. | map[string]string | false |

[Back to Custom Resources](#custom-resources)