Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: tidy vet fmt lint test ## Run the full verification pipeline (tidy, vet, fmt, lint, test).
all: tidy vet fmt lint fix test ## Run the full verification pipeline (tidy, vet, fmt, lint, fix, test).

##@ Development

Expand All @@ -31,6 +31,10 @@ lint: golangci-lint ## Run golangci-lint against the whole module.
tidy: ## Sync go.mod and go.sum with the module's imports.
go mod tidy

.PHONY: fix
fix: ## Modernize and update Go source code to improve code
go fix ./...

.PHONY: test
test: ## Run unit tests under ./pkg/... with the race detector and coverage enabled.
go test -race -cover ./pkg/...
Expand Down
36 changes: 18 additions & 18 deletions pkg/kstatus/polling/clusterreader/caching_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func TestSync(t *testing.T) {
clusterObjs: map[gkNamespace][]unstructured.Unstructured{
{GroupKind: deploymentGVK.GroupKind(), Namespace: "Foo"}: {
{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "deployment-1",
"namespace": "Foo",
},
Expand All @@ -79,10 +79,10 @@ func TestSync(t *testing.T) {
},
{GroupKind: deploymentGVK.GroupKind(), Namespace: "Bar"}: {
{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "deployment-2",
"namespace": "Bar",
},
Expand All @@ -101,13 +101,13 @@ func TestSync(t *testing.T) {
expectedCached: map[gkNamespace]cacheEntry{
{GroupKind: deploymentGVK.GroupKind(), Namespace: "Foo"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "apps/v1", "kind": "Deployment"},
Object: map[string]any{"apiVersion": "apps/v1", "kind": "Deployment"},
Items: []unstructured.Unstructured{
{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "deployment-1",
"namespace": "Foo",
},
Expand All @@ -118,23 +118,23 @@ func TestSync(t *testing.T) {
},
{GroupKind: rsGVK.GroupKind(), Namespace: "Foo"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "apps/v1", "kind": "ReplicaSet"},
Object: map[string]any{"apiVersion": "apps/v1", "kind": "ReplicaSet"},
},
},
{GroupKind: podGVK.GroupKind(), Namespace: "Foo"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Pod"},
Object: map[string]any{"apiVersion": "v1", "kind": "Pod"},
},
},
{GroupKind: deploymentGVK.GroupKind(), Namespace: "Bar"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "apps/v1", "kind": "Deployment"},
Object: map[string]any{"apiVersion": "apps/v1", "kind": "Deployment"},
Items: []unstructured.Unstructured{
{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "deployment-2",
"namespace": "Bar",
},
Expand All @@ -145,12 +145,12 @@ func TestSync(t *testing.T) {
},
{GroupKind: rsGVK.GroupKind(), Namespace: "Bar"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "apps/v1", "kind": "ReplicaSet"},
Object: map[string]any{"apiVersion": "apps/v1", "kind": "ReplicaSet"},
},
},
{GroupKind: podGVK.GroupKind(), Namespace: "Bar"}: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{"apiVersion": "v1", "kind": "Pod"},
Object: map[string]any{"apiVersion": "v1", "kind": "Pod"},
},
},
},
Expand All @@ -160,12 +160,12 @@ func TestSync(t *testing.T) {
barPodGKN := gkNamespace{GroupKind: podGVK.GroupKind(), Namespace: "Bar"}
// 1001 = 3 pages of 500
barObjs := make([]unstructured.Unstructured, 1001)
for i := 0; i < len(barObjs); i++ {
for i := range barObjs {
barObjs[i] = unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": podGVK.GroupVersion().String(),
"kind": podGVK.Kind,
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": fmt.Sprintf("pod-%d", i),
"namespace": barPodGKN.Namespace,
},
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestSync(t *testing.T) {
expectedCached: map[gkNamespace]cacheEntry{
barPodGKN: {
resources: unstructured.UnstructuredList{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": podGVK.GroupVersion().String(),
"kind": podGVK.Kind,
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/kstatus/polling/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestDeepEqual(t *testing.T) {
Name: "Foo",
},
Resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
Object: map[string]any{
"metadata": map[string]any{
"generation": int64(1),
},
},
Expand All @@ -50,8 +50,8 @@ func TestDeepEqual(t *testing.T) {
Name: "Foo",
},
Resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
Object: map[string]any{
"metadata": map[string]any{
"generation": int64(2),
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/kstatus/polling/statusreaders/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ spec:
`,
listObjects: []unstructured.Unstructured{
{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "ReplicaSet",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": "Foo-12345",
"namespace": "default",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/kstatus/polling/statusreaders/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func newPodWithPhase(name, namespace string, phase corev1.PodPhase) *unstructure

func newUnschedulablePod(name, namespace string) *unstructured.Unstructured {
pod := newPodWithPhase(name, namespace, corev1.PodPending)
_ = unstructured.SetNestedSlice(pod.Object, []interface{}{
map[string]interface{}{
_ = unstructured.SetNestedSlice(pod.Object, []any{
map[string]any{
"type": string(corev1.PodScheduled),
"status": string(corev1.ConditionFalse),
"reason": corev1.PodReasonUnschedulable,
Expand Down
2 changes: 1 addition & 1 deletion pkg/kstatus/polling/testutil/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func YamlToUnstructured(t *testing.T, yml string) *unstructured.Unstructured {
m := make(map[string]interface{})
m := make(map[string]any)
err := yaml.Unmarshal([]byte(yml), &m)
if err != nil {
t.Fatalf("error parsing yaml: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/kstatus/status/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,14 @@ func podConditions(u *unstructured.Unstructured) (*Result, error) {
}
}

func getCrashLoopingContainers(obj map[string]interface{}) ([]string, bool, error) {
func getCrashLoopingContainers(obj map[string]any) ([]string, bool, error) {
var containerNames []string
css, found, err := unstructured.NestedSlice(obj, "status", "containerStatuses")
if !found || err != nil {
return containerNames, found, err
}
for _, item := range css {
cs := item.(map[string]interface{})
cs := item.(map[string]any)
n, found := cs["name"]
if !found {
continue
Expand All @@ -483,13 +483,13 @@ func getCrashLoopingContainers(obj map[string]interface{}) ([]string, bool, erro
if !found {
continue
}
state := s.(map[string]interface{})
state := s.(map[string]any)

ws, found := state["waiting"]
if !found {
continue
}
waitingState := ws.(map[string]interface{})
waitingState := ws.(map[string]any)

r, found := waitingState["reason"]
if !found {
Expand Down
13 changes: 6 additions & 7 deletions pkg/kstatus/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package status
import (
"errors"
"fmt"
"slices"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -52,10 +53,8 @@ func (s Status) String() string {
// not a valid status.
func FromStringOrDie(text string) Status {
s := Status(text)
for _, r := range Statuses {
if s == r {
return s
}
if slices.Contains(Statuses, s) {
return s
}
panic(fmt.Errorf("string has invalid status: %s", s))
}
Expand Down Expand Up @@ -196,15 +195,15 @@ func Augment(u *unstructured.Unstructured) error {
}

if !found {
conditions = make([]interface{}, 0)
conditions = make([]any, 0)
}

currentTime := time.Now().UTC().Format(time.RFC3339)

for _, resCondition := range res.Conditions {
present := false
for _, c := range conditions {
condition, ok := c.(map[string]interface{})
condition, ok := c.(map[string]any)
if !ok {
return errors.New("condition does not have the expected structure")
}
Expand All @@ -228,7 +227,7 @@ func Augment(u *unstructured.Unstructured) error {
}
}
if !present {
conditions = append(conditions, map[string]interface{}{
conditions = append(conditions, map[string]any{
"lastTransitionTime": currentTime,
"lastUpdateTime": currentTime,
"message": resCondition.Message,
Expand Down
17 changes: 8 additions & 9 deletions pkg/kstatus/status/status_augment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ metadata:

var timestamp = time.Now().Add(-1 * time.Minute).UTC().Format(time.RFC3339)

func addConditions(t *testing.T, u *unstructured.Unstructured, conditions []map[string]interface{}) {
conds := make([]interface{}, 0)
func addConditions(t *testing.T, u *unstructured.Unstructured, conditions []map[string]any) {
conds := make([]any, 0)
for _, c := range conditions {
conds = append(conds, c)
}
Expand All @@ -48,12 +48,12 @@ func addConditions(t *testing.T, u *unstructured.Unstructured, conditions []map[
func TestAugmentConditions(t *testing.T) {
testCases := map[string]struct {
manifest string
withConditions []map[string]interface{}
withConditions []map[string]any
expectedConditions []Condition
}{
"no existing conditions": {
manifest: pod,
withConditions: []map[string]interface{}{},
withConditions: []map[string]any{},
expectedConditions: []Condition{
{
Type: ConditionReconciling,
Expand All @@ -64,7 +64,7 @@ func TestAugmentConditions(t *testing.T) {
},
"has other existing conditions": {
manifest: pod,
withConditions: []map[string]interface{}{
withConditions: []map[string]any{
{
"lastTransitionTime": timestamp,
"lastUpdateTime": timestamp,
Expand All @@ -88,7 +88,7 @@ func TestAugmentConditions(t *testing.T) {
},
"already has condition of standard type InProgress": {
manifest: pod,
withConditions: []map[string]interface{}{
withConditions: []map[string]any{
{
"lastTransitionTime": timestamp,
"lastUpdateTime": timestamp,
Expand All @@ -107,7 +107,7 @@ func TestAugmentConditions(t *testing.T) {
},
"already has condition of standard type Failed": {
manifest: pod,
withConditions: []map[string]interface{}{
withConditions: []map[string]any{
{
"lastTransitionTime": timestamp,
"lastUpdateTime": timestamp,
Expand All @@ -126,13 +126,12 @@ func TestAugmentConditions(t *testing.T) {
},
"custom resource with no conditions": {
manifest: custom,
withConditions: []map[string]interface{}{},
withConditions: []map[string]any{},
expectedConditions: []Condition{},
},
}

for tn, tc := range testCases {
tc := tc
t.Run(tn, func(t *testing.T) {
u := y2u(t, tc.manifest)
addConditions(t, u, tc.withConditions)
Expand Down
Loading
Loading