8
8
9
9
"gopkg.in/yaml.v2"
10
10
corev1 "k8s.io/api/core/v1"
11
+ "k8s.io/apimachinery/pkg/api/errors"
11
12
"k8s.io/apimachinery/pkg/types"
12
13
"sigs.k8s.io/controller-runtime/pkg/client"
13
14
)
@@ -21,7 +22,8 @@ const (
21
22
AnnotationErrorRevision = "infra.weave.works/error-revision"
22
23
23
24
// DefaultNamespace will be used if RUNTIME_NAMESPACE is not defined.
24
- DefaultNamespace = "flux-system"
25
+ DefaultNamespace = "flux-system"
26
+ DefaultTokenSecretName = "branch-planner-token"
25
27
)
26
28
27
29
// Example ConfigMap
@@ -34,7 +36,7 @@ const (
34
36
// metadata:
35
37
// name: branch-based-planner
36
38
// data:
37
- // # Secret to use to use GitHub API.
39
+ // # Secret to use GitHub API.
38
40
// # Key in the secret: token
39
41
// secretNamespace: flux-system
40
42
// secretName: bbp-token
@@ -56,23 +58,39 @@ type Config struct {
56
58
Labels map [string ]string
57
59
}
58
60
59
- func ReadConfig (ctx context.Context , clusterClient client.Client , ref types.NamespacedName ) (Config , error ) {
60
- config := Config {}
61
-
62
- if ref .Namespace == "" {
63
- ref .Namespace = RuntimeNamespace ()
61
+ func ReadConfig (ctx context.Context , clusterClient client.Client , configMapObjectKey types.NamespacedName ) (Config , error ) {
62
+ if configMapObjectKey .Namespace == "" {
63
+ configMapObjectKey .Namespace = RuntimeNamespace ()
64
64
}
65
-
66
65
configMap := & corev1.ConfigMap {}
67
- err := clusterClient .Get (ctx , ref , configMap )
66
+ err := clusterClient .Get (ctx , configMapObjectKey , configMap )
68
67
if err != nil {
68
+ defaultConfig := Config {
69
+ SecretName : DefaultTokenSecretName ,
70
+ SecretNamespace : RuntimeNamespace (),
71
+ Resources : []client.ObjectKey {
72
+ {Namespace : RuntimeNamespace ()},
73
+ },
74
+ }
75
+
76
+ // Check for not found error, it's ok to not have a ConfigMap
77
+ if errors .IsNotFound (err ) {
78
+ return defaultConfig , nil
79
+ }
80
+
81
+ // Check for permission error, it's ok to not have access to the ConfigMap
82
+ if errors .IsForbidden (err ) {
83
+ return defaultConfig , nil
84
+ }
85
+
86
+ // Return a generic error for other cases
69
87
return Config {}, fmt .Errorf ("unable to get ConfigMap: %w" , err )
70
88
}
71
89
90
+ config := Config {}
72
91
config .SecretNamespace = configMap .Data ["secretNamespace" ]
73
92
config .SecretName = configMap .Data ["secretName" ]
74
93
resourceData := configMap .Data ["resources" ]
75
-
76
94
if config .SecretNamespace == "" {
77
95
config .SecretNamespace = RuntimeNamespace ()
78
96
}
0 commit comments