forked from opencurve/curve-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opencurve#27): supports a common way for deployment waiting
Signed-off-by: Anur Ijuokarukas <[email protected]>
- Loading branch information
1 parent
ebde98e
commit 2f697e2
Showing
3 changed files
with
127 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package k8sutil | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
v1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// WaitForDeploymentsToStart waits for the deployments to start, and returns a channel to indicate whether | ||
// all deployments are started or not | ||
// | ||
// tickDuration is the interval to check the deployment status | ||
// objectMeta is the metadata of the deployment | ||
// | ||
// we use the hub chan to collect the result of each deployment, and when all deployments are started, | ||
// we return true, otherwise, we return false, this design let WaitForDeploymentToStart and | ||
// WaitForDeploymentsToStart can be used in the same way | ||
func WaitForDeploymentsToStart(ctx context.Context, clientSet kubernetes.Interface, tickDuration time.Duration, | ||
objectMetas ...*v1.Deployment) chan bool { | ||
length := len(objectMetas) | ||
hub := make(chan bool, length) | ||
defer close(hub) | ||
for i := range objectMetas { | ||
objectMata := objectMetas[i] | ||
go func() { | ||
if succeed := <-WaitForDeploymentToStart(ctx, clientSet, tickDuration, objectMata); !succeed { | ||
hub <- false | ||
return | ||
} | ||
}() | ||
} | ||
|
||
chn := make(chan bool) | ||
go func() { | ||
defer close(chn) | ||
for i := 0; i < length; i++ { | ||
if succeed := <-hub; !succeed { | ||
chn <- false | ||
return | ||
} | ||
} | ||
chn <- true | ||
return | ||
}() | ||
return chn | ||
} | ||
|
||
// WaitForDeploymentToStart waits for the deployment to start, and returns a channel to indicate whether | ||
// the deployment is started or not | ||
// | ||
// tickDuration is the interval to check the deployment status | ||
// objectMeta is the metadata of the deployment | ||
func WaitForDeploymentToStart(ctx context.Context, clientSet kubernetes.Interface, tickDuration time.Duration, | ||
objectMeta *v1.Deployment) chan bool { | ||
ticker := time.NewTicker(tickDuration) | ||
defer ticker.Stop() | ||
|
||
chn := make(chan bool) | ||
go func() { | ||
defer close(chn) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
deployment, err := clientSet.AppsV1().Deployments(objectMeta.GetNamespace()).Get(objectMeta.GetName(), | ||
metav1.GetOptions{}) | ||
logger.Infof("waiting for deployment %s starting", deployment.Name) | ||
if err != nil { | ||
|
||
// TODO: return the failed reason is required?? | ||
logger.Errorf("failed to get deployment %s in cluster", objectMeta.GetName()) | ||
chn <- false | ||
return | ||
} | ||
if deployment.Status.ObservedGeneration != deployment.Status.ObservedGeneration && | ||
deployment.Status.UpdatedReplicas > 0 && | ||
deployment.Status.ReadyReplicas > 0 { | ||
logger.Infof("deployment %s has been started", deployment.Name) | ||
chn <- true | ||
return | ||
} | ||
|
||
// TODO: should log the unready reason, e.g. conditions, etc. to help debugging?? | ||
case <-ctx.Done(): | ||
chn <- false | ||
logger.Infof("stop waiting for deployment %s to start due to context is done", objectMeta.GetName()) | ||
return | ||
} | ||
} | ||
}() | ||
return chn | ||
} |