Skip to content

Commit

Permalink
Merge pull request #107 from Skarlso/fix-linter
Browse files Browse the repository at this point in the history
fix: update the linter and fix some warnings
  • Loading branch information
Skarlso authored Aug 16, 2024
2 parents b3366d2 + 512c0c1 commit 209a630
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 36 deletions.
12 changes: 1 addition & 11 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,34 @@ linters:
- containedctx # Struct should not contain context, action does.
- contextcheck
- cyclop # Complex functions are not good.
- deadcode
- depguard
- dogsled
- dupl # Check code duplications.
- execinquery
- exhaustive # Doesn't really make sense.
- exhaustivestruct
- exhaustruct # Doesn't really make sense.
- exportloopref
- forcetypeassert # Priority: that can lead to serious crashes.
- funlen # Break long functions.
- gci
- gochecknoglobals
- gochecknoinits # Init functions cause an import to have side effects,
- goerr113
- err113
- goimports # acts weirdly, dci handles imports anyway
- golint
- gomnd # Give constant values a name with constants.
- ifshort
- interfacebloat
- interfacer
- ireturn # Accept interface, return concrate.
- lll
- loggercheck # Doesn't really make sense.
- maligned
- nestif # Some nexted if statements are 8 or 9 deep.
- nilnil # A function should return either something valuable
- nonamedreturns # Either named return, or use simply `return`.
- nosnakecase
- paralleltest
- rowserrcheck
- scopelint
- sqlclosecheck
- structcheck
- tagliatelle
- testpackage # Blackbox testing is preffered.
- unparam
- varcheck
- varnamelen # m, d, p < These are not so meaningful variables.
- wastedassign
- wrapcheck
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
## Tool Versions
KUSTOMIZE_VERSION ?= v5.0.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
GOLANGCI_LINT_VERSION ?= v1.57.2
GOLANGCI_LINT_VERSION ?= v1.60.1

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down
6 changes: 4 additions & 2 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package v1alpha1

const (
// Helm security access keys.
CaFileKey = "caFile"
CertFileKey = "certFile"
UsernameKey = "username"
PasswordKey = "password"
DockerJSONConfigKey = ".dockerconfigjson"
)

const (
LogLevelDebug = 4
)
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

webhookPort := 9443
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
WebhookServer: webhook.NewServer(webhook.Options{
Port: 9443,
Port: webhookPort,
}),
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
Expand Down
30 changes: 13 additions & 17 deletions internal/controller/bootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,18 @@ func (r *BootstrapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

temp, err := os.MkdirTemp("", "crd")
if err != nil {
err := fmt.Errorf("failed to create temp folder: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "TempFolderFailedToCreate", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "TempFolderFailedToCreate", "failed to create temp directory: %s", err)

return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("failed to create temp directory: %w", err)
}

// should probably return a file system / single YAML. Because they can be super large, it's
// not vise to store it in memory as a buffer.
location, err := r.SourceProvider.FetchCRD(ctx, temp, obj, revision)
if err != nil {
err := fmt.Errorf("failed to fetch source: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "CRDFetchFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "CRDFetchFailed", "failed to fetch source: %s", err)

return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("failed to fetch source: %w", err)
}

defer func() {
Expand All @@ -163,18 +161,16 @@ func (r *BootstrapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

sm, err := r.NewResourceManager(ctx, obj)
if err != nil {
err := fmt.Errorf("failed to create resource manager: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "ResourceManagerCreateFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "ResourceManagerCreateFailed", "failed to create resource manager: %s", err)

return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("failed to create resource manager: %w", err)
}

objects, err := readObjects(location)
if err != nil {
err := fmt.Errorf("failed to construct objects to apply: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "ReadingObjectsToApplyFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "ReadingObjectsToApplyFailed", "failed to construct objects to apply: %s", err)

return ctrl.Result{}, err
return ctrl.Result{}, fmt.Errorf("failed to construct objects to apply: %w", err)
}

applied := obj.Status.LastAppliedCRDNames
Expand All @@ -192,25 +188,25 @@ func (r *BootstrapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

if err := r.validateObjects(ctx, obj, objects); err != nil {
if !obj.Spec.ContinueOnValidationError {
conditions.MarkFalse(obj, meta.ReadyCondition, "CRDValidationFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "CRDValidationFailed", "validation failed to on the crd template: %s", err)
logger.Error(err, "validation failed to the CRD for the provided template")

return ctrl.Result{}, nil
return ctrl.Result{}, err
}

logger.Error(err, "validation failed for the CRD, but continue is set so we'll ignore this error")
}

if _, err := sm.ApplyAllStaged(ctx, objects, ssa.DefaultApplyOptions()); err != nil {
err := fmt.Errorf("failed to apply manifests: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "ApplyingCRDSFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "ApplyingCRDSFailed", "failed to apply all stages: %s", err)

return ctrl.Result{}, fmt.Errorf("failed to apply all stages: %w", err)
}

if err = sm.Wait(objects, ssa.DefaultWaitOptions()); err != nil {
err := fmt.Errorf("failed to wait for objects to be ready: %w", err)
conditions.MarkFalse(obj, meta.ReadyCondition, "WaitingOnObjectsFailed", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, "WaitingOnObjectsFailed", "failed to wait for applied objects: %s", err)

return ctrl.Result{}, fmt.Errorf("failed to wait for applied objects: %w", err)
}
Expand Down Expand Up @@ -248,7 +244,7 @@ func (r *BootstrapReconciler) reconcileDelete(ctx context.Context, obj *v1alpha1
logger.Info("found number of crds to clean", "number", len(crds.Items))

for _, item := range crds.Items {
logger.V(4).Info("removed CRD", "crd", item.GetName())
logger.V(v1alpha1.LogLevelDebug).Info("removed CRD", "crd", item.GetName())
if err := r.Delete(ctx, &item); err != nil {
return fmt.Errorf("failed to delete object with name %s: %w", item.GetName(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/source/configmap/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func (s *Source) FetchCRD(ctx context.Context, dir string, obj *v1alpha1.Bootstr
}

file := filepath.Join(dir, "crd.yaml")
if err := os.WriteFile(file, []byte(content), 0o600); err != nil {
const perm = 0o600
if err := os.WriteFile(file, []byte(content), perm); err != nil {
return "", fmt.Errorf("failed to create crd file from config map: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/source/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func (s *Source) getLatestVersion(ctx context.Context, obj *v1alpha1.Bootstrap)
}
}

c.Timeout = 15 * time.Second
const duration = 15 * time.Second
c.Timeout = duration

baseAPIURL := obj.Spec.Source.GitHub.BaseAPIURL
if baseAPIURL == "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/source/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func (s *Source) getLatestVersion(ctx context.Context, obj *v1alpha1.Bootstrap)
}
}

c.Timeout = 15 * time.Second
const duration = 15 * time.Second
c.Timeout = duration

baseAPIURL := obj.Spec.Source.GitLab.BaseAPIURL
if baseAPIURL == "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/source/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (s *Source) FetchCRD(ctx context.Context, dir string, obj *v1alpha1.Bootstr
}

tempHelm := filepath.Join(dir, "helm-temp")
if err := os.MkdirAll(tempHelm, 0o755); err != nil {
const perm = 0o755
if err := os.MkdirAll(tempHelm, perm); err != nil {
return "", fmt.Errorf("failed to create temp helm folder: %w", err)
}
defer os.RemoveAll(tempHelm)
Expand Down

0 comments on commit 209a630

Please sign in to comment.