Skip to content

Commit

Permalink
only syncing all for type opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
manedurphy committed Jul 27, 2021
1 parent 556c35e commit ca3d44d
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
name: secretproviderclasses.secrets-store.csi.x-k8s.io
spec:
group: secrets-store.csi.x-k8s.io
names:
kind: SecretProviderClass
listKind: SecretProviderClassList
plural: secretproviderclasses
singular: secretproviderclass
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: SecretProviderClass is the Schema for the secretproviderclasses API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: SecretProviderClassSpec defines the desired state of SecretProviderClass
properties:
parameters:
additionalProperties:
type: string
description: Configuration for specific provider
type: object
provider:
description: Configuration for provider name
type: string
secretObjects:
items:
description: SecretObject defines the desired state of synced K8s secret objects
properties:
syncAll:
description: SyncAll can sync all secrets defined in the parameters field of SecretProviderClass
type: boolean
data:
items:
description: SecretObjectData defines the desired state of synced K8s secret object data
properties:
key:
description: data field to populate
type: string
objectName:
description: name of the object to sync
type: string
type: object
type: array
labels:
additionalProperties:
type: string
description: labels of K8s secret object
type: object
annotations:
additionalProperties:
type: string
description: annotations of K8s secret object
type: object
secretName:
description: name of the K8s secret object
type: string
type:
description: type of K8s secret object
type: string
type: object
type: array
type: object
status:
description: SecretProviderClassStatus defines the observed state of SecretProviderClass
properties:
byPod:
items:
description: ByPodStatus defines the state of SecretProviderClass as seen by an individual controller
properties:
id:
description: id of the pod that wrote the status
type: string
namespace:
description: namespace of the pod that wrote the status
type: string
type: object
type: array
type: object
type: object
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.0
creationTimestamp: null
name: secretproviderclasspodstatuses.secrets-store.csi.x-k8s.io
spec:
group: secrets-store.csi.x-k8s.io
names:
kind: SecretProviderClassPodStatus
listKind: SecretProviderClassPodStatusList
plural: secretproviderclasspodstatuses
singular: secretproviderclasspodstatus
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: SecretProviderClassPodStatus is the Schema for the secretproviderclassespodstatus API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
status:
description: SecretProviderClassPodStatusStatus defines the observed state of SecretProviderClassPodStatus
properties:
mounted:
type: boolean
objects:
items:
description: SecretProviderClassObject defines the object fetched from external secrets store
properties:
id:
type: string
version:
type: string
type: object
type: array
podName:
type: string
secretProviderClassName:
type: string
targetPath:
type: string
type: object
type: object
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
20 changes: 12 additions & 8 deletions controllers/secretproviderclasspodstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ import (
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/secretutil"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
client "sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apiruntime "k8s.io/apimachinery/pkg/runtime"

"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
Expand All @@ -66,7 +65,7 @@ const (
type SecretProviderClassPodStatusReconciler struct {
client.Client
mutex *sync.Mutex
scheme *apiruntime.Scheme
scheme *runtime.Scheme
nodeID string
reader client.Reader
writer client.Writer
Expand Down Expand Up @@ -136,7 +135,7 @@ func (r *SecretProviderClassPodStatusReconciler) Patcher(ctx context.Context) er
spcMap[namespace+"/"+spcName] = *spc
}
// get the pod and check if the pod has a owner reference
pod := &v1.Pod{}
pod := &corev1.Pod{}
err = r.reader.Get(ctx, client.ObjectKey{Namespace: namespace, Name: spcPodStatuses[i].Status.PodName}, pod)
if err != nil {
return fmt.Errorf("failed to fetch pod during patching, err: %+v", err)
Expand Down Expand Up @@ -239,7 +238,7 @@ func (r *SecretProviderClassPodStatusReconciler) Reconcile(ctx context.Context,

// Obtain the full pod metadata. An object reference is needed for sending
// events and the UID is helpful for validating the SPCPS TargetPath.
pod := &v1.Pod{}
pod := &corev1.Pod{}
if err := r.reader.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName}, pod); err != nil {
klog.ErrorS(err, "failed to get pod", "pod", klog.ObjectRef{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName})
if apierrors.IsNotFound(err) {
Expand All @@ -250,7 +249,7 @@ func (r *SecretProviderClassPodStatusReconciler) Reconcile(ctx context.Context,
// skip reconcile if the pod is being terminated
// or the pod is in succeeded state (for jobs that complete aren't gc yet)
// or the pod is in a failed state (all containers get terminated)
if !pod.GetDeletionTimestamp().IsZero() || pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
if !pod.GetDeletionTimestamp().IsZero() || pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
klog.V(5).InfoS("pod is being terminated, skipping reconcile", "pod", klog.KObj(pod))
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -288,6 +287,10 @@ func (r *SecretProviderClassPodStatusReconciler) Reconcile(ctx context.Context,

for _, secretObj := range spc.Spec.SecretObjects {
if secretObj.SyncAll {
if secretutil.GetSecretType(strings.TrimSpace(secretObj.Type)) != corev1.SecretTypeOpaque {
return ctrl.Result{}, fmt.Errorf("secret provider class %s/%s cannot use syncAll for non-opaque secrets", spc.Namespace, spc.Name)
}

spcutil.BuildSecretObjectData(files, secretObj)
}
}
Expand Down Expand Up @@ -436,6 +439,7 @@ func (r *SecretProviderClassPodStatusReconciler) createK8sSecret(ctx context.Con
}

err := r.writer.Create(ctx, secret)

if err == nil {
klog.InfoS("successfully created Kubernetes secret", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
return nil
Expand Down Expand Up @@ -490,7 +494,7 @@ func (r *SecretProviderClassPodStatusReconciler) patchSecretWithOwnerRef(ctx con

// secretExists checks if the secret with name and namespace already exists
func (r *SecretProviderClassPodStatusReconciler) secretExists(ctx context.Context, name, namespace string) (bool, error) {
o := &v1.Secret{}
o := &corev1.Secret{}
secretKey := types.NamespacedName{
Namespace: namespace,
Name: name,
Expand Down
4 changes: 4 additions & 0 deletions pkg/rotation/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid

for _, secretObj := range spc.Spec.SecretObjects {
if secretObj.SyncAll {
if secretutil.GetSecretType(strings.TrimSpace(secretObj.Type)) != v1.SecretTypeOpaque {
return fmt.Errorf("secret provider class %s/%s cannot use syncAll for non-opaque secrets", spc.Namespace, spc.Name)
}

spcutil.BuildSecretObjectData(files, secretObj)
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/util/spcutil/secret_object_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ func BuildSecretObjectData(files map[string]string, secretObj *v1alpha1.SecretOb
ObjectName: key,
Key: renamedKey,
})

}
}

0 comments on commit ca3d44d

Please sign in to comment.