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
26 changes: 25 additions & 1 deletion internal/k8s/selfregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,35 @@ func (a *AdmissionWebhookRegisterClient) Register(ctx context.Context, c *config
{
Operations: []admissionv1.OperationType{admissionv1.Create, admissionv1.Update},
Rule: admissionv1.Rule{
APIGroups: []string{"extensions"},
APIGroups: []string{"apps"},
APIVersions: []string{"v1"},
Resources: []string{"deployments"},
},
},
{
Operations: []admissionv1.OperationType{admissionv1.Create, admissionv1.Update},
Rule: admissionv1.Rule{
APIGroups: []string{"apps"},
APIVersions: []string{"v1"},
Resources: []string{"statefulsets"},
},
},
{
Operations: []admissionv1.OperationType{admissionv1.Create, admissionv1.Update},
Rule: admissionv1.Rule{
APIGroups: []string{"apps"},
APIVersions: []string{"v1"},
Resources: []string{"daemonsets"},
},
},
{
Operations: []admissionv1.OperationType{admissionv1.Create, admissionv1.Update},
Rule: admissionv1.Rule{
APIGroups: []string{"apps"},
APIVersions: []string{"v1"},
Resources: []string{"replicasets"},
},
},
},
SideEffects: &sideEffects,
AdmissionReviewVersions: []string{"v1"},
Expand Down
52 changes: 41 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ type admissionWebhookServer struct {
logger *zap.SugaredLogger
}

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

func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
var resp = &admissionv1.AdmissionResponse{
UID: in.UID,
Expand Down Expand Up @@ -100,43 +106,67 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis

func (s *admissionWebhookServer) unmarshal(in *admissionv1.AdmissionRequest) (p string, meta *v1.ObjectMeta, spec *corev1.PodSpec) {
var podSpec *corev1.PodSpec
var metaPtr *v1.ObjectMeta
var metaPtr, podMetaPtr *v1.ObjectMeta
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":

var deployment appsv1.Deployment
metaPtr = &deployment.Spec.Template.ObjectMeta
metaPtr = &deployment.ObjectMeta
podMetaPtr = &deployment.Spec.Template.ObjectMeta
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":

var pod corev1.Pod
p = ""
metaPtr = &pod.ObjectMeta
podMetaPtr = &pod.ObjectMeta
podSpec = &pod.Spec
target = &pod
case "DaemonSet":
var daemonSet appsv1.DaemonSet
metaPtr = &daemonSet.Spec.Template.ObjectMeta
metaPtr = &daemonSet.ObjectMeta
podMetaPtr = &daemonSet.Spec.Template.ObjectMeta
podSpec = &daemonSet.Spec.Template.Spec
target = &daemonSet
case "StatefulSet":
var statefulSet appsv1.StatefulSet
metaPtr = &statefulSet.Spec.Template.ObjectMeta
metaPtr = &statefulSet.ObjectMeta
podMetaPtr = &statefulSet.Spec.Template.ObjectMeta
podSpec = &statefulSet.Spec.Template.Spec
target = &statefulSet
case replicaSetKind:
var replicaSet appsv1.ReplicaSet
metaPtr = &replicaSet.ObjectMeta
podMetaPtr = &replicaSet.Spec.Template.ObjectMeta
podSpec = &replicaSet.Spec.Template.Spec
target = &replicaSet
default:
return "", nil, nil
}

if err := json.Unmarshal(in.Object.Raw, target); err != nil {
return "", nil, nil
}
p = path.Join("/", p)
if metaPtr.Labels == nil {
metaPtr.Labels = make(map[string]string)
if podMetaPtr.Labels == nil {
podMetaPtr.Labels = make(map[string]string)
}
// Annotations shouldn't be applied second time.
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.

return p, metaPtr, podSpec

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?


return path.Join("/", p), podMetaPtr, podSpec
}

func (s *admissionWebhookServer) createVolumesPatch(p string, volumes []corev1.Volume) jsonpatch.JsonPatchOperation {
Expand Down