-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: status-check for stateful sets (#6828)
- Loading branch information
1 parent
38ff45a
commit 8f67e8d
Showing
12 changed files
with
634 additions
and
359 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 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
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,18 @@ | ||
apiVersion: apps/v1 | ||
kind: StatefulSet | ||
metadata: | ||
name: zz-image-doesnt-exist | ||
spec: | ||
serviceName: foo | ||
replicas: 2 | ||
selector: | ||
matchLabels: | ||
app: apply-status-check-failure | ||
template: | ||
metadata: | ||
labels: | ||
app: apply-status-check-failure | ||
spec: | ||
containers: | ||
- name: apply-status-check-failure | ||
image: zz-image-doesnt-exist |
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,49 @@ | ||
/* | ||
Copyright 2021 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validator | ||
|
||
import ( | ||
"context" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type statefulSetPodsSelector struct { | ||
k kubernetes.Interface | ||
setObj appsv1.StatefulSet | ||
} | ||
|
||
func NewStatefulSetPodsSelector(k kubernetes.Interface, d appsv1.StatefulSet) PodSelector { | ||
return &statefulSetPodsSelector{k, d} | ||
} | ||
|
||
func (s *statefulSetPodsSelector) Select(ctx context.Context, ns string, opts metav1.ListOptions) ([]v1.Pod, error) { | ||
pods, err := s.k.CoreV1().Pods(ns).List(ctx, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var filtered []v1.Pod | ||
for _, po := range pods.Items { | ||
if isPodOwnedBy(po, &s.setObj) { | ||
filtered = append(filtered, po) | ||
} | ||
} | ||
return filtered, nil | ||
} |
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,130 @@ | ||
/* | ||
Copyright 2021 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
fakekubeclientset "k8s.io/client-go/kubernetes/fake" | ||
appsclient "k8s.io/client-go/kubernetes/typed/apps/v1" | ||
|
||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestStatefulSetPodsSelector(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
allPods []v1.Pod | ||
expectedPods []v1.Pod | ||
}{ | ||
{ | ||
description: "pod don't exist in test namespace", | ||
allPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "foo-ns", | ||
OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}}, | ||
}, | ||
expectedPods: nil, | ||
}, | ||
{ | ||
description: "only statefulset pods", | ||
allPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "test", | ||
OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}}, | ||
expectedPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "test", | ||
OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}}, | ||
}, | ||
{ | ||
description: "only standalone pods", | ||
allPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "test", | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}}, | ||
expectedPods: nil, | ||
}, | ||
{ | ||
description: "standalone pods and statefulset pods", | ||
allPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo1", | ||
Namespace: "test", | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}, { | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo2", | ||
Namespace: "test", | ||
OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}}, | ||
expectedPods: []v1.Pod{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo2", | ||
Namespace: "test", | ||
OwnerReferences: []metav1.OwnerReference{{UID: "", Controller: truePtr()}}, | ||
}, | ||
TypeMeta: metav1.TypeMeta{Kind: "Pod"}, | ||
}}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
t.Override(&getReplicaSet, func(_ *appsv1.Deployment, _ appsclient.AppsV1Interface) ([]*appsv1.ReplicaSet, []*appsv1.ReplicaSet, *appsv1.ReplicaSet, error) { | ||
return nil, nil, &appsv1.ReplicaSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
UID: types.UID(""), | ||
}, | ||
}, nil | ||
}) | ||
var rs []runtime.Object | ||
for i := range test.allPods { | ||
p := test.allPods[i] | ||
rs = append(rs, &p) | ||
} | ||
f := fakekubeclientset.NewSimpleClientset(rs...) | ||
s := NewStatefulSetPodsSelector(f, appsv1.StatefulSet{}) | ||
actualPods, err := s.Select(context.Background(), "test", metav1.ListOptions{}) | ||
t.CheckNoError(err) | ||
t.CheckDeepEqual(test.expectedPods, actualPods) | ||
}) | ||
} | ||
} |
Oops, something went wrong.