-
Notifications
You must be signed in to change notification settings - Fork 16
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 support for ReplicaSet k8s kind #152
Add support for ReplicaSet k8s kind #152
Conversation
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
As part of the task it was found that Job and CronJob K8s resourses can be supported with nsm annotations too, but it is not necessary now so it was not added. |
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
As part of the task the small research was made about usage of Annotations in service meshes, Istio and Consul-k8s. For Consul annotations are widely used for pods, but also they are used for Services and Service Accounts and there are examples with custom kinds. For Istio annotations used also for Pods, Deployments, Services and other types too. |
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
main.go
Outdated
if isReplicaOwnedByDeployment(in.Kind.Kind, metaPtr) { | ||
return "", nil, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anastasia-malysheva Could you say more about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin, replicaSet inherits annotations from the deployment it is owned by. As soon as deployment already has annotations and they've been applied before replicaset was created, replicaset tries to apply same annotations again and it causes errors during installation. That's why this check is here.
main.go
Outdated
return false | ||
} | ||
|
||
func updatePodAnnotations(kind string, metaPtr, podMetaPtr *v1.ObjectMeta) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we cant do this directly in pod branch in the switch case above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the method is already big enough and lint raise errors when I add more code there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fix the linter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used inline function as you proposed and it worked, so there is no need to change linter settings.
main.go
Outdated
podMetaPtr.Annotations = metaPtr.Annotations | ||
} | ||
err := errors.New("can't register a sink factory for empty string") | ||
panic(err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we don't panic? It is not good practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with logger error.
I think this is good. But it requires a small refactor. I see two options:
switch t {
case "pod":
handlePod(...)
...
} |
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
main.go
Outdated
bytes, err := json.Marshal([]jsonpatch.JsonPatchOperation{ | ||
s.createInitContainerPatch(p, annotation, spec.InitContainers), | ||
s.createContainerPatch(p, annotation, spec.Containers), | ||
s.createVolumesPatch(p, spec.Volumes), | ||
s.createLabelPatch(p, metaPtr.Labels), | ||
}) | ||
if err != nil { | ||
s.logger.Info("Some error happened") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s.logger.Info("Some error happened") |
main.go
Outdated
@@ -95,36 +103,48 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis | |||
} | |||
|
|||
resp.Allowed = true | |||
s.logger.Infof("Response") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s.logger.Infof("Response") |
main.go
Outdated
} | ||
return p, metaPtr, podSpec | ||
|
||
func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func() { |
main.go
Outdated
} | ||
s.logger.Errorf("Malformed specification. Annotations can't be provided in several places.") | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}() |
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
@anastasia-malysheva Could you please resolve these comments |
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
main.go
Outdated
if in.Kind.Kind == replicaSetKind { | ||
for _, o := range metaPtr.OwnerReferences { | ||
if o.Kind == deploymentKind { | ||
return "", nil, nil | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this into replicaSetKind case at line 137?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we can't because unmarshal to target is performed after switch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please use a simple defer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix was added.
main.go
Outdated
if in.Kind.Kind != podKind && metaPtr.Annotations != nil { | ||
if podMetaPtr.Annotations == nil { | ||
podMetaPtr.Annotations = metaPtr.Annotations | ||
} | ||
s.logger.Errorf("Malformed specification. Annotations can't be provided in several places.") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this into pod case at line 119?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, we can't because it's not a pod case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please use a simple defer?
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
main.go
Outdated
const ( | ||
deploymentKind string = "Deployment" | ||
podKind string = "Pod" | ||
replicaSetKind string = "ReplicaSet" | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const ( | |
deploymentKind string = "Deployment" | |
podKind string = "Pod" | |
replicaSetKind string = "ReplicaSet" | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anastasia-malysheva Could you please resolve this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin I took a look to the k8s doc and couldn't find constants for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin I changed linter setting and removed constants
main.go
Outdated
var target interface{} | ||
p = "/spec/template" | ||
switch in.Kind.Kind { | ||
case "Deployment": | ||
case deploymentKind: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case deploymentKind: | |
case "Deployment": |
main.go
Outdated
podSpec = &deployment.Spec.Template.Spec | ||
target = &deployment | ||
case "Pod": | ||
case podKind: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case podKind: | |
case "Pod": |
main.go
Outdated
defer func() { | ||
s.logger.Info("Replicaset Defer method") | ||
for _, o := range metaPtr.OwnerReferences { | ||
if o.Kind == deploymentKind { | ||
p, meta, spec = "", nil, nil | ||
} | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still looking overcomplicated.
I think the main problem here that we are doing things in unmarshal
method that actually doesnt related to the operation.
Please move all 'post unmarshal' changes into separate method and use it right afiter unmarshal in the Review function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added changes, please take a look
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
…d-admission-webhook-k8s@main PR link: networkservicemesh/cmd-admission-webhook-k8s#152 Commit: aa7055e Author: Denis Tingaikin Date: 2022-07-20 12:46:24 +0300 Message: - Merge pull request #152 from anastasia-malysheva/support-replicaset Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main PR link: networkservicemesh/sdk#1630 Commit: 78bedfb Author: Network Service Mesh Bot Date: 2024-08-14 08:41:05 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/api@main (#1630) PR link: networkservicemesh/api#152 Commit: 03f6633 Author: Nikita Skrynnik Date: 2024-08-14 20:39:07 +1100 Message: - Delete unused helper functions (#152) * delete some helper functions * delete more hepler functions * delete more unused helper function * fix linter * return som e helper functions * fix CI --------- Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k@main (#448) PR link: networkservicemesh/sdk#1630 Commit: 78bedfb Author: Network Service Mesh Bot Date: 2024-08-14 08:41:05 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/api@main (#1630) PR link: networkservicemesh/api#152 Commit: 03f6633 Author: Nikita Skrynnik Date: 2024-08-14 20:39:07 +1100 Message: - Delete unused helper functions (#152) * delete some helper functions * delete more hepler functions * delete more unused helper function * fix linter * return som e helper functions * fix CI --------- Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> Signed-off-by: NSMBot <nsmbot@networkservicmesh.io> Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
…k-k8s@main PR link: networkservicemesh/sdk-k8s#517 Commit: f9f55b4 Author: Network Service Mesh Bot Date: 2024-08-14 08:46:00 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/sdk@main (#517) PR link: networkservicemesh/sdk#1630 Commit: 78bedfb Author: Network Service Mesh Bot Date: 2024-08-14 08:41:05 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/api@main (#1630) PR link: networkservicemesh/api#152 Commit: 03f6633 Author: Nikita Skrynnik Date: 2024-08-14 20:39:07 +1100 Message: - Delete unused helper functions (#152) * delete some helper functions * delete more hepler functions * delete more unused helper function * fix linter * return som e helper functions * fix CI --------- Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
…k-k8s@main (#449) PR link: networkservicemesh/sdk-k8s#517 Commit: f9f55b4 Author: Network Service Mesh Bot Date: 2024-08-14 08:46:00 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/sdk@main (#517) PR link: networkservicemesh/sdk#1630 Commit: 78bedfb Author: Network Service Mesh Bot Date: 2024-08-14 08:41:05 -0500 Message: - Update go.mod and go.sum to latest version from networkservicemesh/api@main (#1630) PR link: networkservicemesh/api#152 Commit: 03f6633 Author: Nikita Skrynnik Date: 2024-08-14 20:39:07 +1100 Message: - Delete unused helper functions (#152) * delete some helper functions * delete more hepler functions * delete more unused helper function * fix linter * return som e helper functions * fix CI --------- Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> Signed-off-by: NSMBot <nsmbot@networkservicmesh.io> Co-authored-by: NSMBot <nsmbot@networkservicmesh.io>
Signed-off-by: anastasia.malysheva anastasia.malysheva@xored.com