Add inject support to namespaces#3549
Conversation
Added the following changes: * Added func `AppendNsAnnotation()` under `pkg/inject/inject.go` * Added func `AppendNsAnnotations()` under `pkg/inject/inject.go` * Added func `IsNamespace()` for *ResourceConfig at `pkg/inject/inject/go` Signed-off-by: Mayank Shah <mayankshah1614@gmail.com>
6bcf61e to
048acd7
Compare
* Updated linkerd install golden files as theu go through the transform process * Fix inject to make Namespace annotations come through patching (Pothulapati@b49fcdc) * Added additional unit tests with overrideAnnotations * Fixed linting Signed-off-by: Mayank Shah <mayankshah1614@gmail.com>
048acd7 to
184d4ba
Compare
|
This Looks good 💯 @ihcsim can you take a look? |
ihcsim
left a comment
There was a problem hiding this comment.
@Pothulapati @mayankshah1607 Hey guys, thanks for putting time and effort into this. I think the number of new exported functions being introduced in this PR to the pkg/inject package is a bit much. In addition to the existing ones, the ResourceConfig struct now have these functions: WithNsAnnotation(), AppendAnnotations(), AppendAnnotation(), AppendNsAnnotation(), AppendPodAnnotation(). I am very sure I will forget what are their differences in a few weeks time.
I think ultimately, all we needed to do is:
- Unmarshal the namespace YAML into an object
- So that we can add the
linkerd.io/injectannotation to it - Marshal the object back into YAML
- Return any errors and report mssage
- Then profit!
I propose that we don't bother with calling the inject.GetPatch(bool) function, because there are many logic in there there are relevant only to pods. WDYT of the idea of just introduce one new exported function to do the 5 steps outlined above?
diff --git a/cli/cmd/inject.go b/cli/cmd/inject.go
index 8e701344..5c54718c 100644
--- a/cli/cmd/inject.go
+++ b/cli/cmd/inject.go
@@ -147,8 +147,8 @@ func (rt resourceTransformerInject) transform(bytes []byte) ([]byte, []inject.Re
//Add auto-proxy support for namespace configs
if conf.IsNamespace() {
- // Namespace config annotations get appended here
- conf.AppendNsAnnotation(k8s.ProxyInjectAnnotation, k8s.ProxyInjectEnabled)
+ b, err := conf.InjectNamespace()
+ return b, reports, err
}
if rt.injectProxy {
diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go
index 2f0f60a7..de39ea2c 100644
@@ -647,6 +637,15 @@ func (conf *ResourceConfig) injectPodAnnotations(values *patch) {
}
}
+func (conf *ResourceConfig) InjectNamespace() ([]byte, error) {
+ ns, ok := conf.workload.obj.(*corev1.Namespace)
+ if !ok {
+ return nil, errors.New("can't inject namespace. Type assertion failed")
+ }
+ ns.Annotations[k8s.ProxyInjectAnnotation] = k8s.ProxyInjectEnabled
+ return yaml.Marshal(ns)
+}
+
func (conf *ResourceConfig) injectNsAnnotations(values *patch) {
values.AddRootAnnotations = len(conf.pod.meta.Annotations) == 0With this approach, we don't need the additional exported functions and conditionals to get around the pod-relevant injection logic.
Here's the result of my quick test:
⚡ k get ns default -oyaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2019-10-16T22:15:44Z"
name: default
resourceVersion: "151"
selfLink: /api/v1/namespaces/default
uid: f094e0ca-af97-4072-a658-25d9ffd2c531
spec:
finalizers:
- kubernetes
status:
phase: Active
⚡ k get ns default -oyaml| bin/go-run cli inject -
apiVersion: v1
kind: Namespace
metadata:
annotations:
linkerd.io/inject: enabled
creationTimestamp: "2019-10-16T22:15:44Z"
name: default
resourceVersion: "151"
selfLink: /api/v1/namespaces/default
uid: f094e0ca-af97-4072-a658-25d9ffd2c531
spec:
finalizers:
- kubernetes
status:
phase: Active
---
namespace "default" injected
|
Hmm, @ihcsim that would make the logic simple, |
|
I think those options are stored in the |
|
I'll work on these changes and open another PR. |
|
Hey, @ihcsim On logging |
|
@mayankshah1607 do you mind pushing your commits so that we can take a look at the code where the namespace is |
|
Hi @cpretzer . I have pushed my commits - mayankshah1607@24f0c0c |
|
Fixed it. The problem was that Considering all the factors discussed above, I have opened a new PR as this one has too many unnecessary file changes dealing with the Please review my new PR - #3607 |
Fixes #3255
This PR adds support for
linkerd injectto annotate namespaces as follows :kubectl get ns/default -o yaml | linkerd inject -Anything added to the said namespace gets linkerd support.
AppendNsAnnotation()forResourceConfigobjectsIsNamespace()forResourceConfigobjects - checks if a given config is of Kind namespacelinkerd installgolden filesinject.goto make namespace annotations come in through patching - Pothulapati@b49fcdcoverrideAnnotationsas well