Skip to content

Commit

Permalink
improve status check for deployment resourse
Browse files Browse the repository at this point in the history
  • Loading branch information
kudoh committed Feb 4, 2020
1 parent c6716d8 commit 4ec4929
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ resource_types:
type: docker-image
source:
repository: kudohn/concourse-k8s-resource
tag: <version>
tag: 0.0.7
```
### `resources`
Expand Down
2 changes: 2 additions & 0 deletions cmd/out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log"
"os"
"reflect"
"time"
)

var streams = genericclioptions.IOStreams{
Expand Down Expand Up @@ -50,6 +51,7 @@ func main() {
log.Fatalln("cannot run kubectl command", err)
}
if !request.Params.Delete {
time.Sleep(5 * time.Second)
log.Println("check status for", request.Source.WatchResources)
if ok := k8s.CheckResourceStatus(clientset, request.Source.Namespace, request.Source.WatchResources, request.Params.StatusCheckTimeout); !ok {
log.Fatalln("resource is not running...")
Expand Down
21 changes: 13 additions & 8 deletions pkg/k8s/status_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func CheckResourceStatus(clientset kubernetes.Interface, namespace string, resou
if err != nil {
log.Println("status check", "error!", "->", c.resource.Name, err)
return false
} else {
log.Println("status check", "ok!", "->", c.resource.Name)
}
case <-c.timeout:
log.Println("status check", "timeout!", "->", c.resource.Name)
Expand All @@ -75,39 +73,46 @@ func (c *statusChecker) check() error {
return fmt.Errorf("interrupted")
default:
}
var desired int32
var current int32
switch {
case IsDeployment(c.resource.Kind):
d, err := c.clientset.AppsV1().Deployments(c.namespace).Get(c.resource.Name, metav1.GetOptions{})
if err != nil {
return err
}
desired = *d.Spec.Replicas

_, _, newRS, err := deployment.GetAllReplicaSets(d, c.clientset.AppsV1())
if err != nil {
return err
}
if newRS == nil {
log.Printf("[%s] No ReplicaSet found. skip check\n", c.resource.Name)
return nil
continue
}

if newRS.Status.ReadyReplicas == *newRS.Spec.Replicas {
current = newRS.Status.ReadyReplicas
if desired == current {
log.Println("status check", "ok!", "->", c.resource.Name, "desired:", desired, "ready:", current)
return nil
}
case IsStatefulSet(c.resource.Kind):
sts, err := c.clientset.AppsV1().StatefulSets(c.namespace).Get(c.resource.Name, metav1.GetOptions{})
if err != nil {
return err
}
if sts.Status.ReadyReplicas == *sts.Spec.Replicas {
desired = *sts.Spec.Replicas
current = sts.Status.ReadyReplicas
if desired == current {
log.Println("status check", "ok!", "->", c.resource.Name, "desired:", desired, "ready:", current)
return nil
}
default:
log.Fatalln("unsupported resource kind", c.resource.Kind)
}
if i%10 == 0 {
log.Println("waiting", c.resource.Name)
log.Println("waiting", c.resource.Name, "desired:", desired, "ready:", current)
}
time.Sleep(1 * time.Second)
time.Sleep(3 * time.Second)
}
}
6 changes: 3 additions & 3 deletions pkg/k8s/status_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestCheckResourceStatus(t *testing.T) {
UID: "uid",
},
Spec: appv1.DeploymentSpec{
Replicas: replicas(3),
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{},
},
Expand All @@ -38,7 +39,6 @@ func TestCheckResourceStatus(t *testing.T) {
}},
},
Spec: appv1.ReplicaSetSpec{
Replicas: replicas(3),
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{},
},
Expand Down Expand Up @@ -86,6 +86,7 @@ func TestTimeout(t *testing.T) {
UID: "uid",
},
Spec: appv1.DeploymentSpec{
Replicas: replicas(3),
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{},
},
Expand All @@ -103,7 +104,6 @@ func TestTimeout(t *testing.T) {
}},
},
Spec: appv1.ReplicaSetSpec{
Replicas: replicas(3),
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{},
},
Expand All @@ -129,4 +129,4 @@ func replicas(num int) *int32 {

func control(b bool) *bool {
return &b
}
}

0 comments on commit 4ec4929

Please sign in to comment.