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 support for ReplicaSet k8s kind #152

Merged

Conversation

anastasia-malysheva
Copy link
Contributor

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>
Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
@anastasia-malysheva
Copy link
Contributor Author

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>
@anastasia-malysheva
Copy link
Contributor Author

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
Comment on lines 159 to 161
if isReplicaOwnedByDeployment(in.Kind.Kind, metaPtr) {
return "", nil, nil
}
Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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())
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced with logger error.

@denis-tingaikin
Copy link
Member

I think this is good. But it requires a small refactor. I see two options:

  1. make new methods inline and fix linter
  2. create for each type in switch operator a handle function. Something like
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 Fixed Show fixed Hide fixed
main.go Fixed Show fixed Hide fixed
main.go Fixed Show fixed Hide fixed
main.go Fixed Show fixed Hide fixed
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")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
s.logger.Infof("Response")

main.go Outdated
}
return p, metaPtr, podSpec

func() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func() {

main.go Outdated
}
s.logger.Errorf("Malformed specification. Annotations can't be provided in several places.")
}
}()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}()

Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
@denis-tingaikin
Copy link
Member

@anastasia-malysheva Could you please resolve these comments
#152 (comment)
#152 (comment)

Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com>
main.go Outdated
Comment on lines 154 to 160
if in.Kind.Kind == replicaSetKind {
for _, o := range metaPtr.OwnerReferences {
if o.Kind == deploymentKind {
return "", nil, nil
}
}
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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
Comment on lines 162 to 167
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.")
}
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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
Comment on lines 59 to 64
const (
deploymentKind string = "Deployment"
podKind string = "Pod"
replicaSetKind string = "ReplicaSet"
)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const (
deploymentKind string = "Deployment"
podKind string = "Pod"
replicaSetKind string = "ReplicaSet"
)

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
case deploymentKind:
case "Deployment":

main.go Outdated
podSpec = &deployment.Spec.Template.Spec
target = &deployment
case "Pod":
case podKind:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
case podKind:
case "Pod":

main.go Outdated
Comment on lines 143 to 150
defer func() {
s.logger.Info("Replicaset Defer method")
for _, o := range metaPtr.OwnerReferences {
if o.Kind == deploymentKind {
p, meta, spec = "", nil, nil
}
}
}()
Copy link
Member

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.

Copy link
Contributor Author

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>
@denis-tingaikin denis-tingaikin merged commit aa7055e into networkservicemesh:main Jul 20, 2022
nsmbot pushed a commit to networkservicemesh/deployments-k8s that referenced this pull request Jul 20, 2022
…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>
nsmbot pushed a commit that referenced this pull request Aug 14, 2024
…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>
nsmbot added a commit that referenced this pull request Aug 14, 2024
…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>
nsmbot pushed a commit that referenced this pull request Aug 14, 2024
…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>
nsmbot added a commit that referenced this pull request Aug 14, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants