-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update new template and add extract file
- Loading branch information
1 parent
aaddccd
commit 99a2adb
Showing
7 changed files
with
160 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package extract | ||
|
||
import ( | ||
"reflect" | ||
|
||
"golang.stackrox.io/kube-linter/pkg/k8sutil" | ||
appsV1 "k8s.io/api/apps/v1" | ||
) | ||
|
||
func StatefulSetSpec(obj k8sutil.Object) (appsV1.StatefulSetSpec, bool) { | ||
switch obj := obj.(type) { | ||
case *appsV1.StatefulSet: | ||
return obj.Spec, true | ||
default: | ||
|
||
kind := obj.GetObjectKind().GroupVersionKind().Kind | ||
if kind != "StatefulSet" { | ||
return appsV1.StatefulSetSpec{}, false | ||
} | ||
|
||
objValue := reflect.Indirect(reflect.ValueOf(obj)) | ||
spec := objValue.FieldByName("Spec") | ||
if !spec.IsValid() { | ||
return appsV1.StatefulSetSpec{}, false | ||
} | ||
statefulSetSpec, ok := spec.Interface().(appsV1.StatefulSetSpec) | ||
if ok { | ||
return statefulSetSpec, true | ||
} | ||
return appsV1.StatefulSetSpec{}, false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
pkg/templates/volumeclaimtemplates/internal/params/gen-params.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package volumeclaimtemplates | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/volumeclaimtemplates/internal/params" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestStatefulSetVolumeClaimTemplateAnnotation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
annotation string // Adjusted to match the parameter name in Params | ||
wantDiags int | ||
}{ | ||
{"WithAnnotation", "value", 0}, | ||
{"WithoutAnnotation", "", 1}, // Empty string or any value for negative case | ||
} | ||
|
||
|
||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
sts := &appsv1.StatefulSet{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "statefulset"}, | ||
Spec: appsv1.StatefulSetSpec{ | ||
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{ | ||
{ObjectMeta: metav1.ObjectMeta{Annotations: tt.annotations}}, | ||
}, | ||
}, | ||
} | ||
|
||
// Creating mock lint context | ||
mockLintCtx := mocks.NewMockContext() | ||
mockLintCtx.AddObject("statefulset", sts) | ||
|
||
// Fetching template | ||
template, found := templates.Get("statefulset-volumeclaimtemplate-annotation") | ||
if !found { | ||
t.Fatalf("failed to get template") | ||
} | ||
|
||
// Parsing and validating parameters | ||
params, err := params.ParseAndValidate(map[string]interface{}{}) | ||
if err != nil { | ||
t.Fatalf("failed to parse and validate params: %v", err) | ||
} | ||
|
||
// Instantiating check function | ||
checkFunc, err := template.Instantiate(params) | ||
if err != nil { | ||
t.Fatalf("failed to instantiate check function: %v", err) | ||
} | ||
|
||
// Running the check function | ||
diags := checkFunc(mockLintCtx, mockLintCtx.Objects()[0]) | ||
if len(diags) != tt.wantDiags { | ||
t.Errorf("got %d diagnostics, want %d", len(diags), tt.wantDiags) | ||
} | ||
}) | ||
} | ||
} |