1
1
package operator2
2
2
3
3
import (
4
+ "fmt"
5
+
6
+ corev1 "k8s.io/api/core/v1"
7
+ "k8s.io/apimachinery/pkg/api/errors"
4
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5
9
"k8s.io/klog"
6
10
7
11
configv1 "github.com/openshift/api/config/v1"
8
12
)
9
13
14
+ const (
15
+ injectProxyCAName = "config.openshift.io/inject-trusted-cabundle"
16
+ injectProxyCAValue = "true"
17
+ )
18
+
10
19
func (c * authOperator ) handleProxyConfig () * configv1.Proxy {
11
20
proxyConfig , err := c .proxy .Get (globalConfigName , metav1.GetOptions {})
12
21
if err != nil {
@@ -15,3 +24,80 @@ func (c *authOperator) handleProxyConfig() *configv1.Proxy {
15
24
}
16
25
return proxyConfig
17
26
}
27
+
28
+ func (c * authOperator ) handleProxyCA (proxy * configv1.Proxy ) (* corev1.ConfigMap , error ) {
29
+ cmClient := c .configMaps .ConfigMaps (targetNamespace )
30
+ shouldHaveProxyCA := proxy != nil && len (proxy .Spec .TrustedCA .Name ) > 0
31
+
32
+ proxyCACM , err := cmClient .Get (proxyCAName , metav1.GetOptions {})
33
+ if errors .IsNotFound (err ) {
34
+ if ! shouldHaveProxyCA {
35
+ return nil , nil
36
+ }
37
+ proxyCACM , err = cmClient .Create (defaultProxyCACM ())
38
+ }
39
+ if err != nil {
40
+ return nil , err
41
+ }
42
+
43
+ if ! shouldHaveProxyCA {
44
+ return nil , cmClient .Delete (proxyCAName , & metav1.DeleteOptions {})
45
+ }
46
+
47
+ if err := isValidProxyCACM (proxyCACM ); err != nil {
48
+ // delete the proxy CA config map so that it is replaced with the proper one in next reconcile loop
49
+ klog .Infof ("deleting invalid proxy CA config map: %#v" , proxyCACM )
50
+ opts := & metav1.DeleteOptions {Preconditions : & metav1.Preconditions {UID : & proxyCACM .UID }}
51
+ if err := cmClient .Delete (proxyCACM .Name , opts ); err != nil && ! errors .IsNotFound (err ) {
52
+ klog .Infof ("failed to delete invalid proxy CA config map: %v" , err )
53
+ }
54
+ return nil , err
55
+ }
56
+
57
+ return proxyCACM , nil
58
+ }
59
+
60
+ func isValidProxyCACM (cm * corev1.ConfigMap ) error {
61
+ if cm .Annotations [injectProxyCAName ] != injectProxyCAValue {
62
+ return fmt .Errorf ("config map missing injection annotation: %#v" , cm )
63
+ }
64
+ return nil
65
+ }
66
+
67
+ func defaultProxyCACM () * corev1.ConfigMap {
68
+ meta := defaultMeta ()
69
+ meta .Name = proxyCAName
70
+ meta .Annotations [injectProxyCAName ] = injectProxyCAValue
71
+ return & corev1.ConfigMap {
72
+ ObjectMeta : meta ,
73
+ }
74
+ }
75
+
76
+ func appendCombinedCAToVolumesAndMounts (volumes []corev1.Volume , mounts []corev1.VolumeMount , combinedCACM * corev1.ConfigMap ) ([]corev1.Volume , []corev1.VolumeMount ) {
77
+ // nothing got injected in that CM, we shouldn't mount it to trust store
78
+ if combinedCACM == nil || len (combinedCACM .Data ) == 0 {
79
+ return volumes , mounts
80
+ }
81
+
82
+ combinedCAVolume := corev1.Volume {
83
+ Name : proxyCAName ,
84
+ VolumeSource : corev1.VolumeSource {
85
+ ConfigMap : & corev1.ConfigMapVolumeSource {
86
+ LocalObjectReference : corev1.LocalObjectReference {
87
+ Name : proxyCAName ,
88
+ },
89
+ Items : []corev1.KeyToPath {{Key : proxyCAKey , Path : proxyCAVolumePath }},
90
+ },
91
+ },
92
+ }
93
+ volumes = append (volumes , combinedCAVolume )
94
+
95
+ combinedCAMount := corev1.VolumeMount {
96
+ Name : proxyCAName ,
97
+ ReadOnly : true ,
98
+ MountPath : proxyCAMount ,
99
+ }
100
+ mounts = append (mounts , combinedCAMount )
101
+
102
+ return volumes , mounts
103
+ }
0 commit comments