Skip to content

Commit

Permalink
fixup for deployment readiness test
Browse files Browse the repository at this point in the history
  • Loading branch information
Omri Eival committed Apr 13, 2021
1 parent 407478d commit 89d9e1d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
version = "1.3.6"
version = "1.3.7"
)

var port int
Expand Down
6 changes: 4 additions & 2 deletions pkg/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,21 @@ func getAllPods(namespace *string) (*apiv1.PodList, error) {
func waitForReady(name *string, ti time.Time, numPods int32, readyChan chan<- bool) {
go func() {
for {
print(".")
count := int32(0)
pods, err := podsClient.List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err)
os.Exit(1)
}
for _, p := range pods.Items {
if strings.HasPrefix(p.Name, *name) && p.CreationTimestamp.After(ti) && p.Status.Phase == apiv1.PodRunning {
if strings.HasPrefix(p.Name, *name) && p.CreationTimestamp.After(ti.Add(-time.Second)) && p.Status.Phase == apiv1.PodRunning {
count += 1
}
if count == numPods {
readyChan <- true
break
println()
return
}
}
time.Sleep(time.Millisecond * 300)
Expand Down
6 changes: 2 additions & 4 deletions pkg/k8s/exposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
v12 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"time"
)

var supportedSchemes = map[string]v12.Protocol{
Expand Down Expand Up @@ -53,8 +52,7 @@ func ExposeAsService(namespace, name *string, tunnelPort int, scheme string, raw
deployment := newDeployment(*namespace, *name, tunnelPort, image, ctrPorts)

service := newService(*namespace, *name, ports)
creationTime := time.Now().Add(-1 * time.Second)
_, err := deploymentsClient.Create(context.Background(), deployment, v1.CreateOptions{
d, err := deploymentsClient.Create(context.Background(), deployment, v1.CreateOptions{
TypeMeta: v1.TypeMeta{},
DryRun: nil,
FieldManager: "",
Expand All @@ -71,7 +69,7 @@ func ExposeAsService(namespace, name *string, tunnelPort int, scheme string, raw
return err
}
log.Infof("Exposed service's cluster ip is: %s", newSvc.Spec.ClusterIP)
waitForReady(name, creationTime, *deployment.Spec.Replicas, readyChan)
waitForReady(name, d.GetCreationTimestamp().Time, *deployment.Spec.Replicas, readyChan)
return nil
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/k8s/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)

func injectToDeployment(o *appsv1.Deployment, c *apiv1.Container, image string, readyChan chan<- bool) (bool, error) {
Expand All @@ -26,14 +25,14 @@ func injectToDeployment(o *appsv1.Deployment, c *apiv1.Container, image string,
if updateErr != nil {
return false, updateErr
}
waitForReady(&o.Name, o.GetCreationTimestamp().Time, *o.Spec.Replicas, readyChan)
return true, nil
}

func InjectSidecar(namespace, objectName *string, port *int, image string, readyChan chan<- bool) (bool, error) {
log.Infof("Injecting tunnel sidecar to %s/%s", *namespace, *objectName)
getClients(namespace)
co := newContainer(*port, image, []apiv1.ContainerPort{})
creationTime := time.Now().Add(-1 * time.Second)
obj, err := deploymentsClient.Get(context.Background(), *objectName, metav1.GetOptions{})
if err != nil {
return false, err
Expand All @@ -45,8 +44,6 @@ func InjectSidecar(namespace, objectName *string, port *int, image string, ready
if err != nil {
return false, err
}

waitForReady(objectName, creationTime, *obj.Spec.Replicas, readyChan)
return true, nil
}

Expand Down Expand Up @@ -78,19 +75,18 @@ func RemoveSidecar(namespace, objectName *string, image string, readyChan chan<-
if err != nil {
return false, err
}
deletionTime := time.Now().Add(-1 * time.Second)
_, err = removeFromSpec(&obj.Spec.Template.Spec, image)
if err != nil {
return false, err
}
_, updateErr := deploymentsClient.Update(context.Background(), obj, metav1.UpdateOptions{
u, updateErr := deploymentsClient.Update(context.Background(), obj, metav1.UpdateOptions{
TypeMeta: metav1.TypeMeta{},
DryRun: nil,
FieldManager: "",
})
if updateErr != nil {
return false, updateErr
}
waitForReady(objectName, deletionTime, *obj.Spec.Replicas, readyChan)
waitForReady(objectName, u.GetCreationTimestamp().Time, *obj.Spec.Replicas, readyChan)
return true, nil
}

0 comments on commit 89d9e1d

Please sign in to comment.