Skip to content

Commit

Permalink
Mark deployments as not required, fix cert creation
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Jan 9, 2021
1 parent 817c371 commit f9a8357
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
10 changes: 6 additions & 4 deletions pkg/cmd/verify/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (o *Options) Execute() error {
}
if result.CertificateError != nil {
logrus.
Infof("error when waiting for certificate to be ready: %v", err)
Infof("error when waiting for certificate to be ready: %v", result.CertificateError)
return err
}
logrus.Info("ヽ(•‿•)ノ Cert-manager is READY!")
Expand All @@ -105,10 +105,12 @@ func (o *Options) Execute() error {
func formatDeploymentResult(result []verify.DeploymentResult) string {
var formattedResult string
for _, r := range result {
if r.Ready {
formattedResult += fmt.Sprintf("Deployment %s READY! ヽ(•‿•)ノ\n", r.Name)
if r.Status == verify.Ready {
formattedResult += fmt.Sprintf("Deployment %s READY! ヽ(•‿•)ノ\n", r.Deployment.Name)
} else if r.Status == verify.NotReady {
formattedResult += fmt.Sprintf("Deployment %s not ready. Reason: %s\n", r.Deployment.Name, r.Error.Error())
} else {
formattedResult += fmt.Sprintf("Deployment %s not ready. Reason: %s\n", r.Name, r.Error.Error())
formattedResult += fmt.Sprintf("Deployment %s not found. Required?: %t\n", r.Deployment.Name, r.Deployment.Required)
}
}
return formattedResult
Expand Down
7 changes: 3 additions & 4 deletions pkg/verify/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ var namespace = &unstructured.Unstructured{
"metadata": map[string]interface{}{
"name": "cert-manager-test",
},
"spec": map[string]interface{}{
"selfSigned": map[string]interface{}{},
},
},
}

Expand Down Expand Up @@ -60,7 +57,9 @@ func createWithRetry(ctx context.Context, res *unstructured.Unstructured, dynami
return fmt.Errorf("Timeout reached: %v", ctx.Err())
default:
err := createResource(dynamicClient, res)
if err != nil {
if errors.IsAlreadyExists(err) {
logrus.Debugf("Resource %s already exists \n", res.GetName())
} else if err != nil {
logrus.Debugf("Retrying create of resource %s, error: %v\n", res.GetName(), err)
} else {
logrus.Debugf("Resource %s created \n", res.GetName())
Expand Down
53 changes: 36 additions & 17 deletions pkg/verify/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package verify
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,50 +15,68 @@ import (
)

type DeploymentDefinition struct {
Namespace string
Names []string
Namespace string
Deployments []Deployment
}

type Deployment struct {
Name string
Required bool
}

func DeploymentDefinitionDefault(namespace string) DeploymentDefinition {
// TODO make sure these Names work also with helm chart installation
// TODO make sure these Deployments work also with helm chart installation
// TODO make sure we support cert-manager that does not have all these deployments
return DeploymentDefinition{
Namespace: namespace,
Names: []string{"cert-manager", "cert-manager-cainjector", "cert-manager-webhook"},
Namespace: namespace,
Deployments: []Deployment{{"cert-manager", true}, {"cert-manager-cainjector", false}, {"cert-manager-webhook", false}},
}
}

type DeploymentResult struct {
Name string
Ready bool
Deployment Deployment
Status Status
Error error
}

type Status int

const (
NotReady Status = iota
Ready
NotFound
)

// TODO make this configurable
// TODO have a global timeout for all deployments
const defaultPollInterval = 100 * time.Millisecond

func DeploymentsReady(ctx context.Context, kubeClient *kubernetes.Clientset, deployments DeploymentDefinition) []DeploymentResult {
ctx.Deadline()
result := []DeploymentResult{}
for _, d := range deployments.Names {
for _, d := range deployments.Deployments {
if err := ctx.Err(); err != nil {
dr := DeploymentResult{
Name: d,
Deployment: d,
Error: fmt.Errorf("Timeout reached: %v", err),
}
result = append(result, dr)
continue
}

poller := &poller{kubeClient, d, deployments.Namespace}
err := wait.PollImmediateUntil(defaultPollInterval, poller.deploymentReady, ctx.Done())
dr := DeploymentResult{
Name: d,
Ready: true,
Deployment: d,
Status: Ready,
}
_, err := kubeClient.AppsV1().Deployments(deployments.Namespace).Get(context.TODO(), d.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
dr.Status = NotFound
result = append(result, dr)
continue
}
poller := &poller{kubeClient, d, deployments.Namespace}
err = wait.PollImmediateUntil(defaultPollInterval, poller.deploymentReady, ctx.Done())
if err != nil {
dr.Ready = false
dr.Status = NotReady
dr.Error = err
}
result = append(result, dr)
Expand All @@ -67,13 +86,13 @@ func DeploymentsReady(ctx context.Context, kubeClient *kubernetes.Clientset, dep

type poller struct {
kubeClient *kubernetes.Clientset
name string
deployment Deployment
namespace string
}

func (p *poller) deploymentReady() (bool, error) {
statusViewer := &polymorphichelpers.DeploymentStatusViewer{}
cmDeployment, err := p.kubeClient.AppsV1().Deployments(p.namespace).Get(context.TODO(), p.name, metav1.GetOptions{})
cmDeployment, err := p.kubeClient.AppsV1().Deployments(p.namespace).Get(context.TODO(), p.deployment.Name, metav1.GetOptions{})
if err != nil {
return false, fmt.Errorf("error when retrieving cert-manager deployments: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Verify(ctx context.Context, config *rest.Config, options *Options) (*Verify

func allReady(result []DeploymentResult) bool {
for _, r := range result {
if !r.Ready {
if r.Status == NotReady || (r.Status == NotFound && r.Deployment.Required) {
return false
}
}
Expand Down

0 comments on commit f9a8357

Please sign in to comment.