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

Commit

Permalink
🌱 Reenabling more lint tasks
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Hein <[email protected]>
  • Loading branch information
christopherhein committed May 6, 2021
1 parent 3a84c2d commit 6eaa29e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 92 deletions.
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ linters:
- godot
- goerr113
- nestif
# TODO(christopherhein) Reenable these and fix errors
- gosec
- nakedret
- unparam
- staticcheck
- scopelint
# Run with --fast=false for more extensive checks
fast: true
issues:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
output:webhook:dir=./config/webhook \
webhook
## Copy files in CI folders.
mkdir -p ./config/ci/{rbac,manager}
cp -f ./config/rbac/*.yaml ./config/ci/rbac/
cp -f ./config/manager/manager*.yaml ./config/ci/manager/

Expand Down
8 changes: 4 additions & 4 deletions apis/controlplane/v1alpha4/nestedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ func (r *NestedControlPlane) GetOwnerCluster(ctx context.Context, cli client.Cli
return util.GetOwnerCluster(ctx, cli, r.ObjectMeta)
}

func (in *NestedControlPlane) GetConditions() clusterv1.Conditions {
return in.Status.Conditions
func (r *NestedControlPlane) GetConditions() clusterv1.Conditions {
return r.Status.Conditions
}

func (in *NestedControlPlane) SetConditions(conditions clusterv1.Conditions) {
in.Status.Conditions = conditions
func (r *NestedControlPlane) SetConditions(conditions clusterv1.Conditions) {
r.Status.Conditions = conditions
}
6 changes: 0 additions & 6 deletions certificate/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
package certificate

import (
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"fmt"
Expand Down Expand Up @@ -167,8 +166,3 @@ func NewFrontProxyClientCertAndKey(ca *KeyPair) (*KeyPair, error) {
}
return &KeyPair{ProxyClient, frontProxyClientCert, rsaKey, true, true}, nil
}

// newPrivateKey creates an RSA private key
func newPrivateKey() (*rsa.PrivateKey, error) {
return rsa.GenerateKey(cryptorand.Reader, 2048)
}
4 changes: 2 additions & 2 deletions certificate/keypairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (kp KeyPairs) Lookup(ctx context.Context, cli client.Client, clusterName cl
}

// SaveGenerated will save any certificates that have been generated as Kubernetes secrets.
func (c KeyPairs) SaveGenerated(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
for _, keyPair := range c {
func (kp KeyPairs) SaveGenerated(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
for _, keyPair := range kp {
if !keyPair.Generated && !keyPair.New {
continue
}
Expand Down
118 changes: 48 additions & 70 deletions controllers/controlplane/controller_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ func createNestedComponentSts(ctx context.Context,
ncKind clusterv1.ComponentKind,
controlPlaneName, clusterName string, log logr.Logger) error {
var (
ncSts appsv1.StatefulSet
ncSvc corev1.Service
err error
ncSts *appsv1.StatefulSet
ncSvc *corev1.Service
)
// Setup the ownerReferences for all objects
or := metav1.NewControllerRef(&ncMeta,
Expand All @@ -59,43 +58,42 @@ func createNestedComponentSts(ctx context.Context,
// 1. Using the template defined by version/channel to create the
// StatefulSet and the Service
// TODO check the template version/channel, if not set, use the default.
if ncSpec.Version == "" && ncSpec.Channel == "" {
log.V(4).Info("The Version and Channel are not set, " +
"will use the default template.")
ncSts, err = genStatefulSetObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, cli, log)
if err != nil {
return fmt.Errorf("fail to generate the Statefulset object: %v", err)
}
if ncSpec.Version != "" && ncSpec.Channel != "" {
panic("NOT IMPLEMENT YET")
}

log.V(4).Info("The Version and Channel are not set, " +
"will use the default template.")
if err := genStatefulSetObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log, ncSts); err != nil {
return fmt.Errorf("fail to generate the Statefulset object: %v", err)
}

if ncKind != clusterv1.ControllerManager {
// no need to create the service for the NestedControllerManager
ncSvc, err = genServiceObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log)
ncSvc.SetOwnerReferences([]metav1.OwnerReference{*or})
if err != nil {
return fmt.Errorf("fail to generate the Service object: %v", err)
}
if err := cli.Create(ctx, &ncSvc); err != nil {
return err
}
log.Info("successfully create the service for the StatefulSet",
"component", ncKind)
if ncKind != clusterv1.ControllerManager {
// no need to create the service for the NestedControllerManager
if err := genServiceObject(ncMeta, ncSpec, ncKind, controlPlaneName, clusterName, log, ncSvc); err != nil {
return fmt.Errorf("fail to generate the Service object: %v", err)
}

} else {
panic("NOT IMPLEMENT YET")
ncSvc.SetOwnerReferences([]metav1.OwnerReference{*or})
if err := cli.Create(ctx, ncSvc); err != nil {
return err
}
log.Info("successfully create the service for the StatefulSet",
"component", ncKind)
}

// 2. set the NestedComponent object as the owner of the StatefulSet
ncSts.SetOwnerReferences([]metav1.OwnerReference{*or})

// 4. create the NestedComponent StatefulSet
return cli.Create(ctx, &ncSts)
return cli.Create(ctx, ncSts)
}

// genServiceObject generates the Service object corresponding to the
// NestedComponent
func genServiceObject(ncMeta metav1.ObjectMeta,
ncSpec clusterv1.NestedComponentSpec, ncKind clusterv1.ComponentKind,
controlPlaneName, clusterName string, log logr.Logger) (ncSvc corev1.Service, retErr error) {
controlPlaneName, clusterName string, log logr.Logger, svc *corev1.Service) error {
var templateURL string
if ncSpec.Version == "" && ncSpec.Channel == "" {
switch ncKind {
Expand All @@ -111,33 +109,23 @@ func genServiceObject(ncMeta metav1.ObjectMeta,
}
svcTmpl, err := fetchTemplate(templateURL)
if err != nil {
retErr = fmt.Errorf("fail to fetch the default template "+
return fmt.Errorf("fail to fetch the default template "+
"for the %s service: %v", ncKind, err)
return
}

templateCtx := getTemplateArgs(ncMeta, controlPlaneName, clusterName)

svcStr, err := substituteTemplate(templateCtx, svcTmpl)
if err != nil {
retErr = fmt.Errorf("fail to substitute the default template "+
return fmt.Errorf("fail to substitute the default template "+
"for the nestedetcd Service: %v", err)
return
}
rawSvcObj, err := yamlToObject([]byte(svcStr))
if err != nil {
retErr = fmt.Errorf("fail to convert yaml file to Serivce: %v", err)
return
if err := yamlToObject([]byte(svcStr), svc); err != nil {
return fmt.Errorf("fail to convert yaml file to Serivce: %v", err)
}
log.Info("deserialize yaml to runtime object(Service)")
svcObj, ok := rawSvcObj.(*corev1.Service)
if !ok {
retErr = fmt.Errorf("fail to convert runtime object to Serivce")
return
}
ncSvc = *svcObj
log.Info("convert runtime object to Service.")
return

return nil
}

// genStatefulSetObject generates the StatefulSet object corresponding to the
Expand All @@ -146,7 +134,7 @@ func genStatefulSetObject(
ncMeta metav1.ObjectMeta,
ncSpec clusterv1.NestedComponentSpec,
ncKind clusterv1.ComponentKind, controlPlaneName, clusterName string,
cli ctrlcli.Client, log logr.Logger) (ncSts appsv1.StatefulSet, retErr error) {
log logr.Logger, ncSts *appsv1.StatefulSet) error {
var templateURL string
if ncSpec.Version == "" && ncSpec.Channel == "" {
log.V(4).Info("The Version and Channel are not set, " +
Expand All @@ -168,57 +156,47 @@ func genStatefulSetObject(
// 1 fetch the statefulset template
stsTmpl, err := fetchTemplate(templateURL)
if err != nil {
retErr = fmt.Errorf("fail to fetch the default template "+
return fmt.Errorf("fail to fetch the default template "+
"for the %s StatefulSet: %v", ncKind, err)
return
}
// 2 substitute the statefulset template
templateCtx := getTemplateArgs(ncMeta, controlPlaneName, clusterName)
stsStr, err := substituteTemplate(templateCtx, stsTmpl)
if err != nil {
retErr = fmt.Errorf("fail to substitute the default template "+
return fmt.Errorf("fail to substitute the default template "+
"for the %s StatefulSet: %v", ncKind, err)
return
}
// 3 deserialize the yaml string to the StatefulSet object
rawObj, err := yamlToObject([]byte(stsStr))
if err != nil {
retErr = fmt.Errorf("fail to convert yaml file to StatefulSet: %v", err)
return

if err := yamlToObject([]byte(stsStr), ncSts); err != nil {
return fmt.Errorf("fail to convert yaml file to StatefulSet: %v", err)
}
log.V(5).Info("deserialize yaml to runtime object(StatefulSet)")
// 4 convert runtime Object to StatefulSet
stsObj, ok := rawObj.(*appsv1.StatefulSet)
if !ok {
retErr = fmt.Errorf("fail to convert runtime object to StatefulSet")
return
}
log.V(5).Info("convert runtime object to StatefulSet.")

// 5 apply NestedComponent.Spec.Resources and NestedComponent.Spec.Replicas
// to the NestedComponent StatefulSet
for i := range stsObj.Spec.Template.Spec.Containers {
stsObj.Spec.Template.Spec.Containers[i].Resources =
for i := range ncSts.Spec.Template.Spec.Containers {
ncSts.Spec.Template.Spec.Containers[i].Resources =
ncSpec.Resources
}
if ncSpec.Replicas != 0 {
stsObj.Spec.Replicas = &ncSpec.Replicas
ncSts.Spec.Replicas = &ncSpec.Replicas
}
log.V(5).Info("The NestedEtcd StatefulSet's Resources and "+
"Replicas fields are set",
"StatefulSet", stsObj.GetName())
"StatefulSet", ncSts.GetName())

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

// 7 TODO validate the patch and apply it to the template.
ncSts = *stsObj
return
return nil
}

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

// yamlToObject deserialize the yaml to the runtime object
func yamlToObject(yamlContent []byte) (runtime.Object, error) {
func yamlToObject(yamlContent []byte, obj runtime.Object) error {
decode := serializer.NewCodecFactory(scheme.Scheme).
UniversalDeserializer().Decode
obj, _, err := decode(yamlContent, nil, nil)
_, _, err := decode(yamlContent, nil, obj)
if err != nil {
return nil, err
return err
}
return obj, nil
return nil
}

// substituteTemplate substitutes the template contents with the context
Expand Down
6 changes: 3 additions & 3 deletions controllers/controlplane/nestedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *NestedControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager) error
Complete(r)
}

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

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

// reconcileDelete will delete the control plane and all it's nestedcomponents
func (r *NestedControlPlaneReconciler) reconcileDelete(ctx context.Context, log logr.Logger, cluster *clusterv1.Cluster, ncp *controlplanev1.NestedControlPlane) (ctrl.Result, error) {
func (r *NestedControlPlaneReconciler) reconcileDelete(ctx context.Context, log logr.Logger, ncp *controlplanev1.NestedControlPlane) (ctrl.Result, error) {
patchHelper, err := patch.NewHelper(ncp, r.Client)
if err != nil {
log.Error(err, "Failed to configure the patch helper")
Expand Down
3 changes: 2 additions & 1 deletion controllers/infrastructure/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ var _ = BeforeSuite(func() {
ErrorIfCRDPathMissing: true,
}

cfg, err := testEnv.Start()
var err error
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

Expand Down

0 comments on commit 6eaa29e

Please sign in to comment.