Skip to content

Commit

Permalink
feat: status-check for stateful sets (#6828)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsquared94 authored Nov 10, 2021
1 parent 38ff45a commit 8f67e8d
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 359 deletions.
14 changes: 14 additions & 0 deletions docs/content/en/api/skaffold.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -416,6 +417,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -620,6 +622,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -806,6 +809,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -1092,6 +1096,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -1278,6 +1283,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -1494,6 +1500,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -1680,6 +1687,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -1897,6 +1905,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -2083,6 +2092,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -2330,6 +2340,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -2528,6 +2539,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -2726,6 +2738,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down Expand Up @@ -3128,6 +3141,7 @@
"STATUSCHECK_DEPLOYMENT_FETCH_ERR",
"STATUSCHECK_STANDALONE_PODS_FETCH_ERR",
"STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR",
"STATUSCHECK_STATEFULSET_FETCH_ERR",
"STATUSCHECK_POD_INITIALIZING",
"STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS",
"STATUSCHECK_CONFIG_CONNECTOR_FAILED",
Expand Down
1 change: 1 addition & 0 deletions docs/content/en/docs/references/api/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ For Cancelled Error code, use range 800 to 850.<br>
| STATUSCHECK_DEPLOYMENT_FETCH_ERR | 412 | |
| STATUSCHECK_STANDALONE_PODS_FETCH_ERR | 413 | |
| STATUSCHECK_CONFIG_CONNECTOR_RESOURCES_FETCH_ERR | 414 | |
| STATUSCHECK_STATEFULSET_FETCH_ERR | 415 | |
| STATUSCHECK_POD_INITIALIZING | 451 | Pod Initializing |
| STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS | 452 | The actual state of the resource has not yet reached the desired state |
| STATUSCHECK_CONFIG_CONNECTOR_FAILED | 453 | The process of reconciling the actual state with the desired state has encountered an error |
Expand Down
25 changes: 20 additions & 5 deletions integration/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,24 @@ func TestRenderApplyHelmDeployment(t *testing.T) {

// Ensure that an intentionally broken deployment fails the status check in `skaffold apply`.
func TestApplyStatusCheckFailure(t *testing.T) {
testutil.Run(t, "ApplyStatusCheckFailure", func(t *testutil.T) {
err := skaffold.Apply("deployment.yaml").InDir("testdata/apply").Run(t.T)

t.CheckError(true, err)
})
tests := []struct {
description string
manifestFile string
}{
{
description: "status check for deployment resources",
manifestFile: "deployment.yaml",
},
{
description: "status check for statefulset resources",
manifestFile: "statefulset.yaml",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
err := skaffold.Apply(test.manifestFile).InDir("testdata/apply").Run(t.T)
skaffold.Delete()
t.CheckError(true, err)
})
}
}
18 changes: 18 additions & 0 deletions integration/testdata/apply/statefulset.yaml
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
49 changes: 49 additions & 0 deletions pkg/diag/validator/statefulset.go
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
}
130 changes: 130 additions & 0 deletions pkg/diag/validator/statefulset_test.go
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)
})
}
}
Loading

0 comments on commit 8f67e8d

Please sign in to comment.