Skip to content

Commit c7438bf

Browse files
authored
Merge pull request #46 from narcis96/narcis/update_logger
2 parents 57b09c5 + 95274d6 commit c7438bf

File tree

2,647 files changed

+407905
-17977
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,647 files changed

+407905
-17977
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: golangci-lint
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
branches:
8+
- main
9+
paths:
10+
- "**/*.go"
11+
pull_request:
12+
13+
permissions:
14+
contents: read
15+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
16+
pull-requests: read
17+
18+
jobs:
19+
golangci:
20+
name: lint
21+
runs-on: ubuntu-latest
22+
23+
env:
24+
GOPRIVATE: "github.com/loft-sh/*"
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Set up Go
30+
uses: actions/setup-go@v4
31+
with:
32+
go-version-file: "go.mod"
33+
cache: false
34+
35+
- name: Build logcheck plugin
36+
run: go build -o "tools/logcheck.so" -mod=vendor -buildmode=plugin sigs.k8s.io/logtools/logcheck/plugin
37+
env:
38+
GOWORK: off
39+
40+
- name: Build golangci-lint
41+
run: go build -o "tools/golangci-lint" -mod=vendor ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint
42+
43+
- name: Run golangci-lint
44+
run: tools/golangci-lint run --out-format=github-actions ./...
45+
env:
46+
LOGCHECK_CONFIG: hack/logcheck.conf

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ testbin/*
3030
# devlopment tmp files
3131
/.devspace/
3232
devvalues.yaml
33-
kubeconfig.yaml
33+
kubeconfig.yaml
34+
35+
/tools

.golangci.yaml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
run:
2+
timeout: 15m
3+
4+
linters:
5+
disable-all: true
6+
enable:
7+
- asasalint
8+
- asciicheck
9+
- bidichk
10+
- decorder
11+
- dupl
12+
- durationcheck
13+
- errcheck
14+
- errchkjson
15+
- errname
16+
- errorlint
17+
- exhaustive
18+
- exportloopref
19+
- ginkgolinter
20+
- gocheckcompilerdirectives
21+
- gocritic
22+
- gofmt
23+
- goimports
24+
- gosimple
25+
- govet
26+
- grouper
27+
- importas
28+
- ineffassign
29+
- logcheck
30+
- makezero
31+
- misspell
32+
- nakedret
33+
- nilnil
34+
- prealloc
35+
- promlinter
36+
- revive
37+
- staticcheck
38+
- stylecheck
39+
- tagalign
40+
- typecheck
41+
- unconvert
42+
- unused
43+
- usestdlibvars
44+
- whitespace
45+
46+
# next linters to be enabled:
47+
48+
# linters to be enabled in the distant future:
49+
# - cyclop
50+
# - dupl
51+
# - funlen
52+
# - interfacebloat
53+
# - predeclared
54+
# - stylecheck
55+
# - wrapcheck
56+
57+
linters-settings:
58+
custom:
59+
logcheck:
60+
path: tools/logcheck.so
61+
description: structured logging checker
62+
original-url: sigs.k8s.io/logtools/logcheck
63+
64+
gofmt:
65+
simplify: true
66+
67+
dupl:
68+
threshold: 400
69+
70+
exhaustive:
71+
check:
72+
- switch
73+
- map
74+
ignore-enum-types: "ResourceName|Atom"
75+
default-signifies-exhaustive: true
76+
77+
importas:
78+
no-unaliased: true
79+
alias:
80+
# Kubernetes
81+
- pkg: k8s\.io/api/(\w+)/(v[\w\d]+)
82+
alias: $1$2
83+
- pkg: k8s\.io/apimachinery/pkg/api/(\w+)/(v[\w\d]+)
84+
alias: $1$2
85+
- pkg: k8s.io/apimachinery/pkg/api/errors
86+
alias: kerrors
87+
- pkg: k8s.io/apimachinery/pkg/apis/meta/internalversion
88+
alias: metainternalversion
89+
90+
tagalign:
91+
order:
92+
- json
93+
- yaml
94+
- xml
95+
- form
96+
97+
issues:
98+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
99+
max-issues-per-linter: 0
100+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
101+
max-same-issues: 0

api/v1alpha1/groupversion_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ limitations under the License.
1515
*/
1616

1717
// Package v1alpha1 contains API Schema definitions for the infrastructure v1alpha1 API group
18-
//+kubebuilder:object:generate=true
19-
//+groupName=infrastructure.cluster.x-k8s.io
18+
// +kubebuilder:object:generate=true
19+
// +groupName=infrastructure.cluster.x-k8s.io
2020
package v1alpha1
2121

2222
import (

controllers/vcluster_controller.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/go-logr/logr"
2829
vclusterhelm "github.com/loft-sh/utils/pkg/helm"
2930
"github.com/loft-sh/vcluster/pkg/util"
3031
"github.com/loft-sh/vcluster/pkg/util/kubeconfig"
31-
"github.com/loft-sh/vcluster/pkg/util/loghelper"
3232
corev1 "k8s.io/api/core/v1"
3333
kerrors "k8s.io/apimachinery/pkg/api/errors"
3434
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -59,7 +59,7 @@ type VClusterReconciler struct {
5959
client.Client
6060
HelmClient helm.Client
6161
HelmSecrets *helm.Secrets
62-
Log loghelper.Logger
62+
Log logr.Logger
6363
Scheme *runtime.Scheme
6464
clusterKindExists bool
6565
}
@@ -80,7 +80,7 @@ const (
8080
)
8181

8282
func (r *VClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
83-
r.Log.Debugf("Reconcile %s", req.NamespacedName)
83+
r.Log.V(1).Info("Reconcile", "namespacedName", req.NamespacedName)
8484

8585
// get virtual cluster object
8686
vCluster := &v1alpha1.VCluster{}
@@ -166,22 +166,29 @@ func (r *VClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
166166
// check if we have to redeploy
167167
err = r.redeployIfNeeded(ctx, vCluster)
168168
if err != nil {
169-
r.Log.Infof("error during virtual cluster deploy %s/%s: %v", vCluster.Namespace, vCluster.Name, err)
169+
r.Log.Error(err, "error during virtual cluster deploy",
170+
"namespace", vCluster.Namespace,
171+
"name", vCluster.Name,
172+
)
170173
conditions.MarkFalse(vCluster, v1alpha1.HelmChartDeployedCondition, "HelmDeployFailed", v1alpha1.ConditionSeverityError, "%v", err)
171174
return ctrl.Result{RequeueAfter: time.Second * 5}, err
172175
}
173176

174177
// check if vcluster is initialized and sync the kubeconfig Secret
175178
restConfig, err := r.syncVClusterKubeconfig(ctx, vCluster)
176179
if err != nil {
177-
r.Log.Debugf("vcluster %s/%s is not ready: %v", vCluster.Namespace, vCluster.Name, err)
180+
r.Log.V(1).Info("vcluster is not ready",
181+
"namespace", vCluster.Namespace,
182+
"name", vCluster.Name,
183+
"err", err,
184+
)
178185
conditions.MarkFalse(vCluster, v1alpha1.KubeconfigReadyCondition, "CheckFailed", v1alpha1.ConditionSeverityWarning, "%v", err)
179186
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
180187
}
181188

182189
vCluster.Status.Ready, err = r.checkReadyz(vCluster, restConfig)
183190
if err != nil || !vCluster.Status.Ready {
184-
r.Log.Debugf("readiness check failed: %v", err)
191+
r.Log.V(1).Info("readiness check failed", "err", err)
185192
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
186193
}
187194

@@ -210,13 +217,16 @@ func (r *VClusterReconciler) reconcilePhase(vCluster *v1alpha1.VCluster) {
210217
}
211218
}
212219

213-
func (r *VClusterReconciler) redeployIfNeeded(ctx context.Context, vCluster *v1alpha1.VCluster) error {
220+
func (r *VClusterReconciler) redeployIfNeeded(_ context.Context, vCluster *v1alpha1.VCluster) error {
214221
// upgrade chart
215222
if vCluster.Generation == vCluster.Status.ObservedGeneration && conditions.IsTrue(vCluster, v1alpha1.HelmChartDeployedCondition) {
216223
return nil
217224
}
218225

219-
r.Log.Debugf("upgrade virtual cluster helm chart %s/%s", vCluster.Namespace, vCluster.Name)
226+
r.Log.V(1).Info("upgrade virtual cluster helm chart",
227+
"namespace", vCluster.Namespace,
228+
"clusterName", vCluster.Name,
229+
)
220230

221231
var chartRepo string
222232
if vCluster.Spec.HelmRelease != nil {
@@ -259,15 +269,19 @@ func (r *VClusterReconciler) redeployIfNeeded(ctx context.Context, vCluster *v1a
259269
}
260270
if vCluster.Spec.KubernetesVersion != nil && *vCluster.Spec.KubernetesVersion != "" {
261271
v := strings.Split(*vCluster.Spec.KubernetesVersion, ".")
272+
if len(v) != 2 && len(v) != 3 {
273+
return fmt.Errorf("invalid value of the .spec.kubernetesVersion field: %s", *vCluster.Spec.KubernetesVersion)
274+
}
262275
if len(v) == 2 {
263276
kVersion.Major = v[0]
264277
kVersion.Minor = v[1]
265-
} else if len(v) == 3 {
278+
} else {
266279
kVersion.Major = v[0]
267280
kVersion.Minor = v[1]
268-
r.Log.Infof("vclusters %s/%s patch version defined in .spec.kubernetesVersion field will be ignored, latest supported patch version will be used", vCluster.Namespace, vCluster.Name)
269-
} else {
270-
return fmt.Errorf("invalid value of the .spec.kubernetesVersion field: %s", *vCluster.Spec.KubernetesVersion)
281+
r.Log.Info("vclusters patch version defined in .spec.kubernetesVersion field will be ignored, latest supported patch version will be used",
282+
"namespace", vCluster.Namespace,
283+
"clusterName", vCluster.Name,
284+
)
271285
}
272286
}
273287

@@ -283,11 +297,14 @@ func (r *VClusterReconciler) redeployIfNeeded(ctx context.Context, vCluster *v1a
283297
Values: values,
284298
}, r.Log)
285299
if err != nil {
286-
return fmt.Errorf("merge values: %v", err)
300+
return fmt.Errorf("merge values: %w", err)
287301
}
288302

289-
r.Log.Infof("Deploy virtual cluster %s/%s with values: %s", vCluster.Namespace, vCluster.Name, values)
290-
303+
r.Log.Info("Deploy virtual cluster",
304+
"namespace", vCluster.Namespace,
305+
"clusterName", vCluster.Name,
306+
"values", values,
307+
)
291308
chartPath := "./" + chartName + "-" + chartVersion + ".tgz"
292309
_, err = os.Stat(chartPath)
293310
if err != nil {
@@ -310,7 +327,7 @@ func (r *VClusterReconciler) redeployIfNeeded(ctx context.Context, vCluster *v1a
310327
err = fmt.Errorf("%v ... ", err.Error()[:512])
311328
}
312329

313-
return fmt.Errorf("error installing / upgrading vcluster: %v", err)
330+
return fmt.Errorf("error installing / upgrading vcluster: %w", err)
314331
}
315332

316333
conditions.MarkTrue(vCluster, v1alpha1.HelmChartDeployedCondition)
@@ -354,7 +371,7 @@ func (r *VClusterReconciler) syncVClusterKubeconfig(ctx context.Context, vCluste
354371
// write kubeconfig to the vcluster.Name+"-kubeconfig" Secret as expected by CAPI convention
355372
kubeConfig, err := GetVClusterKubeConfig(ctx, r.Client, vCluster)
356373
if err != nil {
357-
return nil, fmt.Errorf("can not retrieve kubeconfig: %v", err)
374+
return nil, fmt.Errorf("can not retrieve kubeconfig: %w", err)
358375
}
359376
if len(kubeConfig.Clusters) != 1 {
360377
return nil, fmt.Errorf("unexpected kube config")
@@ -403,7 +420,7 @@ func (r *VClusterReconciler) syncVClusterKubeconfig(ctx context.Context, vCluste
403420
return nil
404421
})
405422
if err != nil {
406-
return nil, fmt.Errorf("can not create a kubeconfig secret: %v", err)
423+
return nil, fmt.Errorf("can not create a kubeconfig secret: %w", err)
407424
}
408425

409426
conditions.MarkTrue(vCluster, v1alpha1.KubeconfigReadyCondition)
@@ -421,7 +438,7 @@ func (r *VClusterReconciler) checkReadyz(vCluster *v1alpha1.VCluster, restConfig
421438
Transport: transport,
422439
}
423440
resp, err := client.Get(fmt.Sprintf("https://%s:%d/readyz", vCluster.Spec.ControlPlaneEndpoint.Host, vCluster.Spec.ControlPlaneEndpoint.Port))
424-
r.Log.Debugf("%s/%s: ready check took: %v", vCluster.Namespace, vCluster.Name, time.Since(t))
441+
r.Log.V(1).Info("ready check done", "namespace", vCluster.Namespace, "name", vCluster.Name, "duration", time.Since(t))
425442
if err != nil {
426443
return false, err
427444
}
@@ -437,7 +454,7 @@ func (r *VClusterReconciler) checkReadyz(vCluster *v1alpha1.VCluster, restConfig
437454
return true, nil
438455
}
439456

440-
func DiscoverHostFromService(ctx context.Context, client client.Client, vCluster *v1alpha1.VCluster) (string, error) {
457+
func DiscoverHostFromService(_ context.Context, client client.Client, vCluster *v1alpha1.VCluster) (string, error) {
441458
host := ""
442459
err := wait.PollImmediate(time.Second*2, time.Second*10, func() (done bool, err error) {
443460
service := &corev1.Service{}
@@ -472,7 +489,7 @@ func DiscoverHostFromService(ctx context.Context, client client.Client, vCluster
472489
return true, nil
473490
})
474491
if err != nil {
475-
return "", fmt.Errorf("can not get vcluster service: %v", err)
492+
return "", fmt.Errorf("can not get vcluster service: %w", err)
476493
}
477494

478495
if host == "" {
@@ -497,7 +514,7 @@ func GetVClusterKubeConfig(ctx context.Context, clusterClient client.Client, vCl
497514

498515
kubeConfig, err := clientcmd.Load(kcBytes)
499516
if err != nil {
500-
return nil, fmt.Errorf("failed to load vcluster kube config: %v", err)
517+
return nil, fmt.Errorf("failed to load vcluster kube config: %w", err)
501518
}
502519

503520
return kubeConfig, nil
@@ -535,7 +552,10 @@ func (r *VClusterReconciler) deleteHelmChart(ctx context.Context, namespace, nam
535552
return nil
536553
}
537554

538-
r.Log.Debugf("delete vcluster %s/%s helm release", namespace, name)
555+
r.Log.Info("delete vcluster helm release",
556+
"namespace", namespace,
557+
"name", name,
558+
)
539559
return r.HelmClient.Delete(name, namespace)
540560
}
541561

0 commit comments

Comments
 (0)