88
99 "gopkg.in/yaml.v2"
1010 corev1 "k8s.io/api/core/v1"
11+ "k8s.io/apimachinery/pkg/api/errors"
1112 "k8s.io/apimachinery/pkg/types"
1213 "sigs.k8s.io/controller-runtime/pkg/client"
1314)
@@ -21,7 +22,8 @@ const (
2122 AnnotationErrorRevision = "infra.weave.works/error-revision"
2223
2324 // DefaultNamespace will be used if RUNTIME_NAMESPACE is not defined.
24- DefaultNamespace = "flux-system"
25+ DefaultNamespace = "flux-system"
26+ DefaultTokenSecretName = "branch-planner-token"
2527)
2628
2729// Example ConfigMap
@@ -34,7 +36,7 @@ const (
3436// metadata:
3537// name: branch-based-planner
3638// data:
37- // # Secret to use to use GitHub API.
39+ // # Secret to use GitHub API.
3840// # Key in the secret: token
3941// secretNamespace: flux-system
4042// secretName: bbp-token
@@ -56,23 +58,39 @@ type Config struct {
5658 Labels map [string ]string
5759}
5860
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 ()
6464 }
65-
6665 configMap := & corev1.ConfigMap {}
67- err := clusterClient .Get (ctx , ref , configMap )
66+ err := clusterClient .Get (ctx , configMapObjectKey , configMap )
6867 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
6987 return Config {}, fmt .Errorf ("unable to get ConfigMap: %w" , err )
7088 }
7189
90+ config := Config {}
7291 config .SecretNamespace = configMap .Data ["secretNamespace" ]
7392 config .SecretName = configMap .Data ["secretName" ]
7493 resourceData := configMap .Data ["resources" ]
75-
7694 if config .SecretNamespace == "" {
7795 config .SecretNamespace = RuntimeNamespace ()
7896 }
0 commit comments