Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit e9cd910

Browse files
authored
Merge pull request #49 from christopherhein/reenable-presubmit-lint
🌱 Reenabling more Lint tasks
2 parents d114deb + 4f39b4f commit e9cd910

File tree

8 files changed

+63
-93
lines changed

8 files changed

+63
-93
lines changed

.golangci.yml

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ linters:
1515
- godot
1616
- goerr113
1717
- nestif
18-
# TODO(christopherhein) Reenable these and fix errors
19-
- gosec
20-
- nakedret
21-
- unparam
22-
- staticcheck
23-
- scopelint
2418
# Run with --fast=false for more extensive checks
2519
fast: true
2620
issues:

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ test: ## Run tests.
9090
## --------------------------------------
9191
## Binaries
9292
## --------------------------------------
93+
.PHONY: binaries
94+
binaries: managers
9395

9496
.PHONY: manager
95-
manager: ## Build manager binary
97+
manager-core: ## Build manager binary
9698
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/manager sigs.k8s.io/cluster-api-provider-nested
9799

98100
.PHONY: managers
@@ -163,6 +165,7 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
163165
output:webhook:dir=./config/webhook \
164166
webhook
165167
## Copy files in CI folders.
168+
mkdir -p ./config/ci/{rbac,manager}
166169
cp -f ./config/rbac/*.yaml ./config/ci/rbac/
167170
cp -f ./config/manager/manager*.yaml ./config/ci/manager/
168171

apis/controlplane/v1alpha4/nestedcontrolplane_types.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ func (r *NestedControlPlane) GetOwnerCluster(ctx context.Context, cli client.Cli
126126
return util.GetOwnerCluster(ctx, cli, r.ObjectMeta)
127127
}
128128

129-
func (in *NestedControlPlane) GetConditions() clusterv1.Conditions {
130-
return in.Status.Conditions
129+
func (r *NestedControlPlane) GetConditions() clusterv1.Conditions {
130+
return r.Status.Conditions
131131
}
132132

133-
func (in *NestedControlPlane) SetConditions(conditions clusterv1.Conditions) {
134-
in.Status.Conditions = conditions
133+
func (r *NestedControlPlane) SetConditions(conditions clusterv1.Conditions) {
134+
r.Status.Conditions = conditions
135135
}

certificate/helpers.go

-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ limitations under the License.
1414
package certificate
1515

1616
import (
17-
cryptorand "crypto/rand"
1817
"crypto/rsa"
1918
"crypto/x509"
2019
"fmt"
@@ -167,8 +166,3 @@ func NewFrontProxyClientCertAndKey(ca *KeyPair) (*KeyPair, error) {
167166
}
168167
return &KeyPair{ProxyClient, frontProxyClientCert, rsaKey, true, true}, nil
169168
}
170-
171-
// newPrivateKey creates an RSA private key
172-
func newPrivateKey() (*rsa.PrivateKey, error) {
173-
return rsa.GenerateKey(cryptorand.Reader, 2048)
174-
}

certificate/keypairs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (kp KeyPairs) Lookup(ctx context.Context, cli client.Client, clusterName cl
4949
}
5050

5151
// SaveGenerated will save any certificates that have been generated as Kubernetes secrets.
52-
func (c KeyPairs) SaveGenerated(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
53-
for _, keyPair := range c {
52+
func (kp KeyPairs) SaveGenerated(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
53+
for _, keyPair := range kp {
5454
if !keyPair.Generated && !keyPair.New {
5555
continue
5656
}

controllers/controlplane/controller_util.go

+48-70
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ func createNestedComponentSts(ctx context.Context,
4848
ncKind clusterv1.ComponentKind,
4949
controlPlaneName, clusterName string, log logr.Logger) error {
5050
var (
51-
ncSts appsv1.StatefulSet
52-
ncSvc corev1.Service
53-
err error
51+
ncSts *appsv1.StatefulSet
52+
ncSvc *corev1.Service
5453
)
5554
// Setup the ownerReferences for all objects
5655
or := metav1.NewControllerRef(&ncMeta,
@@ -59,43 +58,42 @@ func createNestedComponentSts(ctx context.Context,
5958
// 1. Using the template defined by version/channel to create the
6059
// StatefulSet and the Service
6160
// TODO check the template version/channel, if not set, use the default.
62-
if ncSpec.Version == "" && ncSpec.Channel == "" {
63-
log.V(4).Info("The Version and Channel are not set, " +
64-
"will use the default template.")
65-
ncSts, err = genStatefulSetObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, cli, log)
66-
if err != nil {
67-
return fmt.Errorf("fail to generate the Statefulset object: %v", err)
68-
}
61+
if ncSpec.Version != "" && ncSpec.Channel != "" {
62+
panic("NOT IMPLEMENT YET")
63+
}
64+
65+
log.V(4).Info("The Version and Channel are not set, " +
66+
"will use the default template.")
67+
if err := genStatefulSetObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log, ncSts); err != nil {
68+
return fmt.Errorf("fail to generate the Statefulset object: %v", err)
69+
}
6970

70-
if ncKind != clusterv1.ControllerManager {
71-
// no need to create the service for the NestedControllerManager
72-
ncSvc, err = genServiceObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log)
73-
ncSvc.SetOwnerReferences([]metav1.OwnerReference{*or})
74-
if err != nil {
75-
return fmt.Errorf("fail to generate the Service object: %v", err)
76-
}
77-
if err := cli.Create(ctx, &ncSvc); err != nil {
78-
return err
79-
}
80-
log.Info("successfully create the service for the StatefulSet",
81-
"component", ncKind)
71+
if ncKind != clusterv1.ControllerManager {
72+
// no need to create the service for the NestedControllerManager
73+
if err := genServiceObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log, ncSvc); err != nil {
74+
return fmt.Errorf("fail to generate the Service object: %v", err)
8275
}
8376

84-
} else {
85-
panic("NOT IMPLEMENT YET")
77+
ncSvc.SetOwnerReferences([]metav1.OwnerReference{*or})
78+
if err := cli.Create(ctx, ncSvc); err != nil {
79+
return err
80+
}
81+
log.Info("successfully create the service for the StatefulSet",
82+
"component", ncKind)
8683
}
84+
8785
// 2. set the NestedComponent object as the owner of the StatefulSet
8886
ncSts.SetOwnerReferences([]metav1.OwnerReference{*or})
8987

9088
// 4. create the NestedComponent StatefulSet
91-
return cli.Create(ctx, &ncSts)
89+
return cli.Create(ctx, ncSts)
9290
}
9391

9492
// genServiceObject generates the Service object corresponding to the
9593
// NestedComponent
9694
func genServiceObject(ncMeta metav1.ObjectMeta,
9795
ncSpec clusterv1.NestedComponentSpec, ncKind clusterv1.ComponentKind,
98-
controlPlaneName, clusterName string, log logr.Logger) (ncSvc corev1.Service, retErr error) {
96+
controlPlaneName, clusterName string, log logr.Logger, svc *corev1.Service) error {
9997
var templateURL string
10098
if ncSpec.Version == "" && ncSpec.Channel == "" {
10199
switch ncKind {
@@ -111,33 +109,23 @@ func genServiceObject(ncMeta metav1.ObjectMeta,
111109
}
112110
svcTmpl, err := fetchTemplate(templateURL)
113111
if err != nil {
114-
retErr = fmt.Errorf("fail to fetch the default template "+
112+
return fmt.Errorf("fail to fetch the default template "+
115113
"for the %s service: %v", ncKind, err)
116-
return
117114
}
118115

119116
templateCtx := getTemplateArgs(ncMeta, controlPlaneName, clusterName)
120117

121118
svcStr, err := substituteTemplate(templateCtx, svcTmpl)
122119
if err != nil {
123-
retErr = fmt.Errorf("fail to substitute the default template "+
120+
return fmt.Errorf("fail to substitute the default template "+
124121
"for the nestedetcd Service: %v", err)
125-
return
126122
}
127-
rawSvcObj, err := yamlToObject([]byte(svcStr))
128-
if err != nil {
129-
retErr = fmt.Errorf("fail to convert yaml file to Serivce: %v", err)
130-
return
123+
if err := yamlToObject([]byte(svcStr), svc); err != nil {
124+
return fmt.Errorf("fail to convert yaml file to Serivce: %v", err)
131125
}
132126
log.Info("deserialize yaml to runtime object(Service)")
133-
svcObj, ok := rawSvcObj.(*corev1.Service)
134-
if !ok {
135-
retErr = fmt.Errorf("fail to convert runtime object to Serivce")
136-
return
137-
}
138-
ncSvc = *svcObj
139-
log.Info("convert runtime object to Service.")
140-
return
127+
128+
return nil
141129
}
142130

143131
// genStatefulSetObject generates the StatefulSet object corresponding to the
@@ -146,7 +134,7 @@ func genStatefulSetObject(
146134
ncMeta metav1.ObjectMeta,
147135
ncSpec clusterv1.NestedComponentSpec,
148136
ncKind clusterv1.ComponentKind, controlPlaneName, clusterName string,
149-
cli ctrlcli.Client, log logr.Logger) (ncSts appsv1.StatefulSet, retErr error) {
137+
log logr.Logger, ncSts *appsv1.StatefulSet) error {
150138
var templateURL string
151139
if ncSpec.Version == "" && ncSpec.Channel == "" {
152140
log.V(4).Info("The Version and Channel are not set, " +
@@ -168,57 +156,47 @@ func genStatefulSetObject(
168156
// 1 fetch the statefulset template
169157
stsTmpl, err := fetchTemplate(templateURL)
170158
if err != nil {
171-
retErr = fmt.Errorf("fail to fetch the default template "+
159+
return fmt.Errorf("fail to fetch the default template "+
172160
"for the %s StatefulSet: %v", ncKind, err)
173-
return
174161
}
175162
// 2 substitute the statefulset template
176163
templateCtx := getTemplateArgs(ncMeta, controlPlaneName, clusterName)
177164
stsStr, err := substituteTemplate(templateCtx, stsTmpl)
178165
if err != nil {
179-
retErr = fmt.Errorf("fail to substitute the default template "+
166+
return fmt.Errorf("fail to substitute the default template "+
180167
"for the %s StatefulSet: %v", ncKind, err)
181-
return
182168
}
183169
// 3 deserialize the yaml string to the StatefulSet object
184-
rawObj, err := yamlToObject([]byte(stsStr))
185-
if err != nil {
186-
retErr = fmt.Errorf("fail to convert yaml file to StatefulSet: %v", err)
187-
return
170+
171+
if err := yamlToObject([]byte(stsStr), ncSts); err != nil {
172+
return fmt.Errorf("fail to convert yaml file to StatefulSet: %v", err)
188173
}
189174
log.V(5).Info("deserialize yaml to runtime object(StatefulSet)")
190-
// 4 convert runtime Object to StatefulSet
191-
stsObj, ok := rawObj.(*appsv1.StatefulSet)
192-
if !ok {
193-
retErr = fmt.Errorf("fail to convert runtime object to StatefulSet")
194-
return
195-
}
196-
log.V(5).Info("convert runtime object to StatefulSet.")
175+
197176
// 5 apply NestedComponent.Spec.Resources and NestedComponent.Spec.Replicas
198177
// to the NestedComponent StatefulSet
199-
for i := range stsObj.Spec.Template.Spec.Containers {
200-
stsObj.Spec.Template.Spec.Containers[i].Resources =
178+
for i := range ncSts.Spec.Template.Spec.Containers {
179+
ncSts.Spec.Template.Spec.Containers[i].Resources =
201180
ncSpec.Resources
202181
}
203182
if ncSpec.Replicas != 0 {
204-
stsObj.Spec.Replicas = &ncSpec.Replicas
183+
ncSts.Spec.Replicas = &ncSpec.Replicas
205184
}
206185
log.V(5).Info("The NestedEtcd StatefulSet's Resources and "+
207186
"Replicas fields are set",
208-
"StatefulSet", stsObj.GetName())
187+
"StatefulSet", ncSts.GetName())
209188

210189
// 6 set the "--initial-cluster" command line flag for the Etcd container
211190
if ncKind == clusterv1.Etcd {
212191
icaVal := genInitialClusterArgs(1, clusterName, clusterName, ncMeta.GetNamespace())
213-
stsArgs := append(stsObj.Spec.Template.Spec.Containers[0].Args,
192+
stsArgs := append(ncSts.Spec.Template.Spec.Containers[0].Args,
214193
"--initial-cluster", icaVal)
215-
stsObj.Spec.Template.Spec.Containers[0].Args = stsArgs
194+
ncSts.Spec.Template.Spec.Containers[0].Args = stsArgs
216195
log.V(5).Info("The '--initial-cluster' command line option is set")
217196
}
218197

219198
// 7 TODO validate the patch and apply it to the template.
220-
ncSts = *stsObj
221-
return
199+
return nil
222200
}
223201

224202
func getTemplateArgs(ncMeta metav1.ObjectMeta, controlPlaneName, clusterName string) map[string]string {
@@ -231,14 +209,14 @@ func getTemplateArgs(ncMeta metav1.ObjectMeta, controlPlaneName, clusterName str
231209
}
232210

233211
// yamlToObject deserialize the yaml to the runtime object
234-
func yamlToObject(yamlContent []byte) (runtime.Object, error) {
212+
func yamlToObject(yamlContent []byte, obj runtime.Object) error {
235213
decode := serializer.NewCodecFactory(scheme.Scheme).
236214
UniversalDeserializer().Decode
237-
obj, _, err := decode(yamlContent, nil, nil)
215+
_, _, err := decode(yamlContent, nil, obj)
238216
if err != nil {
239-
return nil, err
217+
return err
240218
}
241-
return obj, nil
219+
return nil
242220
}
243221

244222
// substituteTemplate substitutes the template contents with the context

controllers/controlplane/nestedcontrolplane_controller.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (r *NestedControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) error
6565
Complete(r)
6666
}
6767

68-
// Reconcile is ths main process which will handle updating teh NCP
68+
// Reconcile is ths main process which will handle updating the NCP
6969
func (r *NestedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
7070
log := r.Log.WithValues("nestedcontrolplane", req.NamespacedName)
7171
log.Info("Reconciling NestedControlPlane...")
@@ -118,7 +118,7 @@ func (r *NestedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
118118
// TODO(christopherhein) handle deletion
119119
if !ncp.ObjectMeta.DeletionTimestamp.IsZero() {
120120
// Handle deletion reconciliation loop.
121-
return r.reconcileDelete(ctx, log, cluster, ncp)
121+
return r.reconcileDelete(ctx, log, ncp)
122122
}
123123

124124
defer func() {
@@ -132,7 +132,7 @@ func (r *NestedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
132132
}
133133

134134
// reconcileDelete will delete the control plane and all it's nestedcomponents
135-
func (r *NestedControlPlaneReconciler) reconcileDelete(ctx context.Context, log logr.Logger, cluster *clusterv1.Cluster, ncp *controlplanev1.NestedControlPlane) (ctrl.Result, error) {
135+
func (r *NestedControlPlaneReconciler) reconcileDelete(ctx context.Context, log logr.Logger, ncp *controlplanev1.NestedControlPlane) (ctrl.Result, error) {
136136
patchHelper, err := patch.NewHelper(ncp, r.Client)
137137
if err != nil {
138138
log.Error(err, "Failed to configure the patch helper")

controllers/infrastructure/suite_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ var _ = BeforeSuite(func() {
5858
ErrorIfCRDPathMissing: true,
5959
}
6060

61-
cfg, err := testEnv.Start()
61+
var err error
62+
cfg, err = testEnv.Start()
6263
Expect(err).NotTo(HaveOccurred())
6364
Expect(cfg).NotTo(BeNil())
6465

0 commit comments

Comments
 (0)