Skip to content

Commit

Permalink
Merge pull request #457 from matzew/cm_bundle_work_1.12
Browse files Browse the repository at this point in the history
[release-v1.12 ] Trusted CA bundle CM work
  • Loading branch information
matzew authored Dec 19, 2023
2 parents a42dcf4 + d71b1d1 commit 06c1c47
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 0 deletions.
23 changes: 23 additions & 0 deletions config/openshift-trusted-cabundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-openshift-trusted-cabundle
namespace: knative-eventing
labels:
app.kubernetes.io/version: devel
app.kubernetes.io/name: knative-eventing
config.openshift.io/inject-trusted-cabundle: "true"
30 changes: 30 additions & 0 deletions openshift/patches/023-configmap-trusted-cabundle.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
diff --git a/config/openshift-trusted-cabundle.yaml b/config/openshift-trusted-cabundle.yaml
new file mode 100644
index 000000000..a4c1a5f73
--- /dev/null
+++ b/config/openshift-trusted-cabundle.yaml
@@ -0,0 +1,23 @@
+# Copyright 2020 The Knative Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: config-openshift-trusted-cabundle
+ namespace: knative-eventing
+ labels:
+ app.kubernetes.io/version: devel
+ app.kubernetes.io/name: knative-eventing
+ config.openshift.io/inject-trusted-cabundle: "true"
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
diff --git a/pkg/reconciler/apiserversource/apiserversource.go b/pkg/reconciler/apiserversource/apiserversource.go
index f96a8e4c9..ed1ff2fe9 100644
--- a/pkg/reconciler/apiserversource/apiserversource.go
+++ b/pkg/reconciler/apiserversource/apiserversource.go
@@ -215,6 +215,12 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1.ApiServer
msg := "Deployment created"
if err != nil {
msg = fmt.Sprint("Deployment created, error:", err)
+ } else {
+ // make CM only on clean creation
+ err := r.ensureCaTrustBundleConfigMap(ctx, src, adapterArgs)
+ if err != nil {
+ return nil, err
+ }
}
controller.GetEventRecorder(ctx).Eventf(src, corev1.EventTypeNormal, apiserversourceDeploymentCreated, "%s", msg)
return ra, err
@@ -235,6 +241,20 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1.ApiServer
return ra, nil
}

+func (r *Reconciler) ensureCaTrustBundleConfigMap(ctx context.Context, src *v1.ApiServerSource, adapterArgs resources.ReceiveAdapterArgs) error {
+ _, err := r.kubeClientSet.CoreV1().ConfigMaps(src.Namespace).Get(ctx, resources.TrustedCAConfigMapName, metav1.GetOptions{})
+ if apierrors.IsNotFound(err) {
+ trustedBundleCM := resources.MakeTrustedCABundleConfigMap(&adapterArgs)
+
+ _, err := r.kubeClientSet.CoreV1().ConfigMaps(src.Namespace).Create(ctx, trustedBundleCM, metav1.CreateOptions{})
+ if err != nil && !apierrors.IsAlreadyExists(err) {
+ return fmt.Errorf("error creating trusted CA bundle configmap: %v", err)
+ }
+ }
+
+ return nil
+}
+
func (r *Reconciler) podSpecChanged(oldPodSpec corev1.PodSpec, newPodSpec corev1.PodSpec) bool {
if !equality.Semantic.DeepDerivative(newPodSpec, oldPodSpec) {
return true
diff --git a/pkg/reconciler/apiserversource/resources/cabundle_configmap.go b/pkg/reconciler/apiserversource/resources/cabundle_configmap.go
new file mode 100644
index 000000000..5091d3d39
--- /dev/null
+++ b/pkg/reconciler/apiserversource/resources/cabundle_configmap.go
@@ -0,0 +1,31 @@
+package resources
+
+import (
+ corev1 "k8s.io/api/core/v1"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "knative.dev/pkg/kmeta"
+)
+
+const (
+ // user-provided and system CA certificates
+ TrustedCAConfigMapName = "config-openshift-trusted-cabundle"
+ TrustedCAConfigMapVolume = TrustedCAConfigMapName + "-volume"
+ TrustedCAKey = "ca-bundle.crt"
+)
+
+func MakeTrustedCABundleConfigMap(args *ReceiveAdapterArgs) *corev1.ConfigMap {
+ return &corev1.ConfigMap{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: TrustedCAConfigMapName,
+ Namespace: args.Source.Namespace,
+ Labels: map[string]string{
+ "app.kubernetes.io/name": "knative-eventing",
+ // user-provided and system CA certificates
+ "config.openshift.io/inject-trusted-cabundle": "true",
+ },
+ OwnerReferences: []metav1.OwnerReference{
+ *kmeta.NewControllerRef(args.Source),
+ },
+ },
+ }
+}
diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter.go b/pkg/reconciler/apiserversource/resources/receive_adapter.go
index 48ee41eb1..61ab4fbb2 100644
--- a/pkg/reconciler/apiserversource/resources/receive_adapter.go
+++ b/pkg/reconciler/apiserversource/resources/receive_adapter.go
@@ -38,6 +38,10 @@ import (
reconcilersource "knative.dev/eventing/pkg/reconciler/source"
)

+const (
+ OcpTrusedCaBundleMountPath = "/ocp-serverless-custom-certs"
+)
+
// ReceiveAdapterArgs are the arguments needed to create a ApiServer Receive Adapter.
// Every field is required.
type ReceiveAdapterArgs struct {
@@ -85,6 +89,22 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) {
Spec: corev1.PodSpec{
ServiceAccountName: args.Source.Spec.ServiceAccountName,
EnableServiceLinks: ptr.Bool(false),
+ Volumes: []corev1.Volume{
+ {
+ Name: TrustedCAConfigMapVolume,
+ VolumeSource: corev1.VolumeSource{
+ ConfigMap: &corev1.ConfigMapVolumeSource{
+ LocalObjectReference: corev1.LocalObjectReference{Name: TrustedCAConfigMapName},
+ Items: []corev1.KeyToPath{
+ {
+ Key: TrustedCAKey,
+ Path: TrustedCAKey,
+ },
+ },
+ },
+ },
+ },
+ },
Containers: []corev1.Container{
{
Name: "receive-adapter",
@@ -110,6 +130,13 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) {
RunAsNonRoot: ptr.Bool(true),
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
},
+ VolumeMounts: []corev1.VolumeMount{
+ {
+ Name: TrustedCAConfigMapVolume,
+ MountPath: OcpTrusedCaBundleMountPath,
+ ReadOnly: true,
+ },
+ },
},
},
},
diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go
index 424d747e7..9de5c4a79 100644
--- a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go
+++ b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go
@@ -135,6 +135,22 @@ O2dgzikq8iSy1BlRsVw=
Spec: corev1.PodSpec{
ServiceAccountName: "source-svc-acct",
EnableServiceLinks: ptr.Bool(false),
+ Volumes: []corev1.Volume{
+ {
+ Name: TrustedCAConfigMapVolume,
+ VolumeSource: corev1.VolumeSource{
+ ConfigMap: &corev1.ConfigMapVolumeSource{
+ LocalObjectReference: corev1.LocalObjectReference{Name: TrustedCAConfigMapName},
+ Items: []corev1.KeyToPath{
+ {
+ Key: TrustedCAKey,
+ Path: TrustedCAKey,
+ },
+ },
+ },
+ },
+ },
+ },
Containers: []corev1.Container{
{
Name: "receive-adapter",
@@ -196,6 +212,13 @@ O2dgzikq8iSy1BlRsVw=
RunAsNonRoot: ptr.Bool(true),
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
},
+ VolumeMounts: []corev1.VolumeMount{
+ {
+ Name: TrustedCAConfigMapVolume,
+ MountPath: OcpTrusedCaBundleMountPath,
+ ReadOnly: true,
+ },
+ },
},
},
},
--
2.43.0

24 changes: 24 additions & 0 deletions openshift/release/artifacts/eventing-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5176,3 +5176,27 @@ roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: openshift-serverless-view-eventing-configmaps
---
# Copyright 2020 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-openshift-trusted-cabundle
namespace: knative-eventing
labels:
app.kubernetes.io/version: v1.12
app.kubernetes.io/name: knative-eventing
config.openshift.io/inject-trusted-cabundle: "true"
20 changes: 20 additions & 0 deletions pkg/reconciler/apiserversource/apiserversource.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1.ApiServer
msg := "Deployment created"
if err != nil {
msg = fmt.Sprint("Deployment created, error:", err)
} else {
// make CM only on clean creation
err := r.ensureCaTrustBundleConfigMap(ctx, src, adapterArgs)
if err != nil {
return nil, err
}
}
controller.GetEventRecorder(ctx).Eventf(src, corev1.EventTypeNormal, apiserversourceDeploymentCreated, "%s", msg)
return ra, err
Expand All @@ -245,6 +251,20 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1.ApiServer
return ra, nil
}

func (r *Reconciler) ensureCaTrustBundleConfigMap(ctx context.Context, src *v1.ApiServerSource, adapterArgs resources.ReceiveAdapterArgs) error {
_, err := r.kubeClientSet.CoreV1().ConfigMaps(src.Namespace).Get(ctx, resources.TrustedCAConfigMapName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
trustedBundleCM := resources.MakeTrustedCABundleConfigMap(&adapterArgs)

_, err := r.kubeClientSet.CoreV1().ConfigMaps(src.Namespace).Create(ctx, trustedBundleCM, metav1.CreateOptions{})
if err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("error creating trusted CA bundle configmap: %v", err)
}
}

return nil
}

func (r *Reconciler) podSpecChanged(oldPodSpec corev1.PodSpec, newPodSpec corev1.PodSpec) bool {
if !equality.Semantic.DeepDerivative(newPodSpec, oldPodSpec) {
return true
Expand Down
31 changes: 31 additions & 0 deletions pkg/reconciler/apiserversource/resources/cabundle_configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package resources

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/kmeta"
)

const (
// user-provided and system CA certificates
TrustedCAConfigMapName = "config-openshift-trusted-cabundle"
TrustedCAConfigMapVolume = TrustedCAConfigMapName + "-volume"
TrustedCAKey = "ca-bundle.crt"
)

func MakeTrustedCABundleConfigMap(args *ReceiveAdapterArgs) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: TrustedCAConfigMapName,
Namespace: args.Source.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "knative-eventing",
// user-provided and system CA certificates
"config.openshift.io/inject-trusted-cabundle": "true",
},
OwnerReferences: []metav1.OwnerReference{
*kmeta.NewControllerRef(args.Source),
},
},
}
}
27 changes: 27 additions & 0 deletions pkg/reconciler/apiserversource/resources/receive_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
reconcilersource "knative.dev/eventing/pkg/reconciler/source"
)

const (
OcpTrusedCaBundleMountPath = "/ocp-serverless-custom-certs"
)

// ReceiveAdapterArgs are the arguments needed to create a ApiServer Receive Adapter.
// Every field is required.
type ReceiveAdapterArgs struct {
Expand Down Expand Up @@ -85,6 +89,22 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) {
Spec: corev1.PodSpec{
ServiceAccountName: args.Source.Spec.ServiceAccountName,
EnableServiceLinks: ptr.Bool(false),
Volumes: []corev1.Volume{
{
Name: TrustedCAConfigMapVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: TrustedCAConfigMapName},
Items: []corev1.KeyToPath{
{
Key: TrustedCAKey,
Path: TrustedCAKey,
},
},
},
},
},
},
Containers: []corev1.Container{
{
Name: "receive-adapter",
Expand All @@ -110,6 +130,13 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) {
RunAsNonRoot: ptr.Bool(true),
Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: TrustedCAConfigMapVolume,
MountPath: OcpTrusedCaBundleMountPath,
ReadOnly: true,
},
},
},
},
},
Expand Down
Loading

0 comments on commit 06c1c47

Please sign in to comment.