This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
/
helpers.go
69 lines (60 loc) · 2.17 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright 2020 Critical Stack, LLC
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
http://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.
*/
package controllers
import (
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
bootstrapv1 "github.com/criticalstack/cluster-api-bootstrap-provider-crit/apis/bootstrap/v1alpha2"
)
// storeBootstrapData creates a new secret with the data passed in as input,
// sets the reference in the configuration status and ready to true.
func (r *CritConfigReconciler) storeBootstrapData(ctx context.Context, config *bootstrapv1.CritConfig, cluster *clusterv1.Cluster, data []byte) error {
logger := r.Log.WithValues("critconfig", fmt.Sprintf("%s/%s", config.Namespace, config.Name))
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: config.Name,
Namespace: config.Namespace,
Labels: map[string]string{
clusterv1.ClusterLabelName: cluster.Name,
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: bootstrapv1.GroupVersion.String(),
Kind: "CritConfig",
Name: config.Name,
UID: config.UID,
Controller: pointer.BoolPtr(true),
},
},
},
Data: map[string][]byte{
"value": data,
},
}
if err := r.Client.Create(ctx, secret); err != nil {
if apierrors.IsAlreadyExists(err) {
logger.Info("bootstrap data already exists")
return nil
}
return errors.Wrapf(err, "failed to create kubeconfig secret for CritConfig %s/%s", config.Namespace, config.Name)
}
config.Status.DataSecretName = pointer.StringPtr(secret.Name)
config.Status.Ready = true
return nil
}