Skip to content

Commit 2a17a88

Browse files
committed
linters: add tests for labels
1 parent 3627b5a commit 2a17a88

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

tests/apichecks/crds_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/google/go-cmp/cmp"
2828

2929
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/codegen"
30+
_ "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/register"
31+
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
3032
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdloader"
3133
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
3234
testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
@@ -36,6 +38,7 @@ import (
3638

3739
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3840
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
41+
"k8s.io/apimachinery/pkg/runtime/schema"
3942
"k8s.io/apimachinery/pkg/util/sets"
4043
"k8s.io/klog/v2"
4144
"sigs.k8s.io/yaml"
@@ -280,6 +283,70 @@ func TestCRDsAcronyms(t *testing.T) {
280283
test.CompareGoldenFile(t, "testdata/exceptions/acronyms.txt", want)
281284
}
282285

286+
// Enforces that required labels are present on our CRDs.
287+
func TestCRDRequiredLabels(t *testing.T) {
288+
crds, err := crdloader.LoadAllCRDs()
289+
if err != nil {
290+
t.Fatalf("error loading crds: %v", err)
291+
}
292+
293+
for _, crd := range crds {
294+
gk := schema.GroupKind{Group: crd.Spec.Group, Kind: crd.Spec.Names.Kind}
295+
296+
if gk.Group == "customize.core.cnrm.cloud.google.com" || gk.Group == "core.cnrm.cloud.google.com" {
297+
// TODO: What labels should these have?
298+
continue
299+
}
300+
301+
if crd.Labels["cnrm.cloud.google.com/managed-by-kcc"] != "true" {
302+
t.Errorf("[labels] crd=%s: missing label %q=%q", crd.Name, "cnrm.cloud.google.com/managed-by-kcc", "true")
303+
}
304+
if crd.Labels["cnrm.cloud.google.com/system"] != "true" {
305+
t.Errorf("[labels] crd=%s: missing label %q=%q", crd.Name, "cnrm.cloud.google.com/system", "true")
306+
}
307+
}
308+
}
309+
310+
// Enforces that reconciler labels (TF/DCL) are consistent on our CRDs.
311+
func TestCRDReconcilerLabels(t *testing.T) {
312+
crds, err := crdloader.LoadAllCRDs()
313+
if err != nil {
314+
t.Fatalf("error loading crds: %v", err)
315+
}
316+
317+
var errs []string
318+
for _, crd := range crds {
319+
gk := schema.GroupKind{Group: crd.Spec.Group, Kind: crd.Spec.Names.Kind}
320+
321+
if gk.Group == "customize.core.cnrm.cloud.google.com" || gk.Group == "core.cnrm.cloud.google.com" {
322+
continue
323+
}
324+
325+
hasTF := crd.Labels["cnrm.cloud.google.com/tf2crd"] == "true"
326+
hasDCL := crd.Labels["cnrm.cloud.google.com/dcl2crd"] == "true"
327+
if hasTF && hasDCL {
328+
errs = append(errs, fmt.Sprintf("[labels] crd=%s: ERROR: has both TF and DCL labels", crd.Name))
329+
}
330+
331+
if registry.IsDirectByGK(gk) {
332+
if hasDCL {
333+
errs = append(errs, fmt.Sprintf("[labels] crd=%s: TODO: make direct default (is direct but has DCL label)", crd.Name))
334+
}
335+
if hasTF {
336+
errs = append(errs, fmt.Sprintf("[labels] crd=%s: TODO: make direct default (is direct but has TF label)", crd.Name))
337+
}
338+
} else if !hasTF && !hasDCL {
339+
errs = append(errs, fmt.Sprintf("[labels] crd=%s: TODO: implement controller (is not direct but has no TF or DCL labels)", crd.Name))
340+
}
341+
}
342+
343+
sort.Strings(errs)
344+
345+
want := strings.Join(errs, "\n")
346+
347+
test.CompareGoldenFile(t, "testdata/exceptions/reconciler-labels.txt", want)
348+
}
349+
283350
// Avoid passing sensitive data as plain text in the CRD
284351
func TestNoSensitiveField(t *testing.T) {
285352
crds, err := crdloader.LoadAllCRDs()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[labels] crd=aiplatformmodels.aiplatform.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
2+
[labels] crd=alloydbclusters.alloydb.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
3+
[labels] crd=apikeyskeys.apikeys.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
4+
[labels] crd=apphubdiscoveredservices.apphub.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
5+
[labels] crd=apphubdiscoveredworkloads.apphub.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
6+
[labels] crd=batchtasks.batch.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
7+
[labels] crd=biglakecatalogs.bigquerybiglake.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
8+
[labels] crd=biglakedatabases.bigquerybiglake.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
9+
[labels] crd=bigquerydatapolicies.bigquerydatapolicy.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
10+
[labels] crd=bigquerydatasets.bigquery.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
11+
[labels] crd=bigquerytables.bigquery.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
12+
[labels] crd=bigtableappprofiles.bigtable.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
13+
[labels] crd=bigtableauthorizedviews.bigtable.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
14+
[labels] crd=bigtablebackups.bigtable.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
15+
[labels] crd=bigtableclusters.bigtable.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
16+
[labels] crd=clouddeploydeploypolicies.clouddeploy.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
17+
[labels] crd=clouddmsconversionworkspaces.clouddms.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
18+
[labels] crd=clouddmsprivateconnections.clouddms.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
19+
[labels] crd=cloudidentitygroups.cloudidentity.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
20+
[labels] crd=cloudidentitymemberships.cloudidentity.cnrm.cloud.google.com: TODO: make direct default (is direct but has DCL label)
21+
[labels] crd=computeforwardingrules.compute.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
22+
[labels] crd=computeinterconnects.compute.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
23+
[labels] crd=computetargettcpproxies.compute.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
24+
[labels] crd=dataflowflextemplatejobs.dataflow.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
25+
[labels] crd=dataprocnodegroups.dataproc.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
26+
[labels] crd=deploycustomtargettypes.clouddeploy.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
27+
[labels] crd=discoveryenginedatastoretargetsites.discoveryengine.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
28+
[labels] crd=discoveryengineengines.discoveryengine.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
29+
[labels] crd=edgecontainermachines.edgecontainer.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
30+
[labels] crd=iamauditconfigs.iam.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
31+
[labels] crd=iampolicies.iam.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
32+
[labels] crd=iampolicymembers.iam.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
33+
[labels] crd=managedkafkaconsumergroups.managedkafka.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
34+
[labels] crd=memorystoreinstances.memorystore.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
35+
[labels] crd=netappbackupvaults.netapp.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
36+
[labels] crd=privatecacapools.privateca.cnrm.cloud.google.com: TODO: make direct default (is direct but has DCL label)
37+
[labels] crd=secretmanagersecrets.secretmanager.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
38+
[labels] crd=secretmanagersecretversions.secretmanager.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
39+
[labels] crd=spannerinstances.spanner.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
40+
[labels] crd=storagemanagedfolders.storage.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)
41+
[labels] crd=tagstagkeys.tags.cnrm.cloud.google.com: TODO: make direct default (is direct but has TF label)
42+
[labels] crd=tpuvirtualmachines.tpu.cnrm.cloud.google.com: TODO: implement controller (is not direct but has no TF or DCL labels)

0 commit comments

Comments
 (0)