Skip to content

Commit

Permalink
restrict the access permissions of kruise-daemon to secrets (#1482)
Browse files Browse the repository at this point in the history
* restrict the access permissions of kruise-daemon to secrets

Signed-off-by: mingzhou.swx <mingzhou.swx@alibaba-inc.com>

* util meta ut

Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>

---------

Signed-off-by: mingzhou.swx <mingzhou.swx@alibaba-inc.com>
Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>
Co-authored-by: mingzhou.swx <mingzhou.swx@alibaba-inc.com>
  • Loading branch information
zmberg and mingzhou.swx committed Jan 4, 2024
1 parent f4e238f commit 6f1b1d4
Show file tree
Hide file tree
Showing 20 changed files with 815 additions and 34 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
echo -e "resources:\n- manager.yaml" > config/manager/kustomization.yaml
$(KUSTOMIZE) build config/daemonconfig | kubectl apply -f -

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -

$(KUSTOMIZE) build config/daemonconfig | kubectl delete -f -

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
Expand Down
11 changes: 10 additions & 1 deletion apis/apps/defaults/v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
v1 "k8s.io/kubernetes/pkg/apis/core/v1"
utilpointer "k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

const (
// ProtectionFinalizer is designed to ensure the GC of resources.
ProtectionFinalizer = "apps.kruise.io/deletion-protection"
)

// SetDefaults_SidecarSet set default values for SidecarSet.
Expand Down Expand Up @@ -372,7 +378,7 @@ func SetDefaultsImageTagPullPolicy(obj *v1alpha1.ImageTagPullPolicy) {
}

// SetDefaults_ImagePullJob set default values for ImagePullJob.
func SetDefaultsImagePullJob(obj *v1alpha1.ImagePullJob) {
func SetDefaultsImagePullJob(obj *v1alpha1.ImagePullJob, addProtection bool) {
if obj.Spec.CompletionPolicy.Type == "" {
obj.Spec.CompletionPolicy.Type = v1alpha1.Always
}
Expand All @@ -388,6 +394,9 @@ func SetDefaultsImagePullJob(obj *v1alpha1.ImagePullJob) {
if obj.Spec.ImagePullPolicy == "" {
obj.Spec.ImagePullPolicy = v1alpha1.PullIfNotPresent
}
if addProtection {
controllerutil.AddFinalizer(obj, ProtectionFinalizer)
}
}

// SetDefaultsImageListPullJob set default values for ImageListPullJob.
Expand Down
3 changes: 3 additions & 0 deletions config/daemonconfig/config/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- namespace.yaml
- rbac.yaml
4 changes: 4 additions & 0 deletions config/daemonconfig/config/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: kruise-daemon-config
29 changes: 29 additions & 0 deletions config/daemonconfig/config/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: kruise-daemon-secret-role
namespace: kruise-daemon-config
rules:
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: kruise-daemon-secret-rolebinding
namespace: kruise-daemon-config
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kruise-daemon-secret-role
subjects:
- kind: ServiceAccount
name: kruise-daemon
namespace: kruise-system
8 changes: 8 additions & 0 deletions config/daemonconfig/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace: kruise-daemon-config
# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
bases:
- config
6 changes: 6 additions & 0 deletions config/default/kruise-daemon-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
labels:
control-plane: controller-manager
name: kruise-daemon-config
12 changes: 6 additions & 6 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Adds namespace to all resources.
namespace: kruise-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
Expand All @@ -12,16 +9,19 @@ namePrefix: kruise-
#commonLabels:
# someName: someValue

resources:
- kruise-daemon-config.yaml

bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
Expand All @@ -30,7 +30,7 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
# - manager_auth_proxy_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- manager_webhook_patch.yaml

Expand Down
3 changes: 3 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Adds namespace to all resources.
namespace: kruise-system

resources:
- manager.yaml
10 changes: 0 additions & 10 deletions config/rbac/daemon_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ rules:
verbs:
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
Expand All @@ -64,14 +62,6 @@ rules:
- get
- patch
- update
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- apps.kruise.io
resources:
Expand Down
3 changes: 3 additions & 0 deletions config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Adds namespace to all resources.
namespace: kruise-system

resources:
- role.yaml
- role_binding.yaml
Expand Down
3 changes: 3 additions & 0 deletions config/webhook/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Adds namespace to all resources.
namespace: kruise-system

resources:
- manifests.yaml
- service.yaml
Expand Down
Loading

0 comments on commit 6f1b1d4

Please sign in to comment.