Skip to content

Commit

Permalink
errors.Wrap() missed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kron4eg committed Feb 27, 2019
1 parent 90025ec commit 66818af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
56 changes: 29 additions & 27 deletions pkg/templates/machinecontroller/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (

// MachineController related constants
const (
MachineControllerNamespace = metav1.NamespaceSystem
MachineControllerAppLabelKey = "app"
MachineControllerAppLabelValue = "machine-controller"
MachineControllerTag = "v1.0.4"

MachineControllerNamespace = metav1.NamespaceSystem
MachineControllerAppLabelKey = "app"
MachineControllerAppLabelValue = "machine-controller"
MachineControllerTag = "v1.0.4"
MachineControllerCredentialsSecretName = "machine-controller-credentials"
)

Expand All @@ -37,6 +36,7 @@ func Deploy(ctx *util.Context) error {
if ctx.Clientset == nil {
return errors.New("kubernetes clientset not initialized")
}

if ctx.APIExtensionClientset == nil {
return errors.New("kubernetes apiextension clientset not initialized")
}
Expand All @@ -48,13 +48,13 @@ func Deploy(ctx *util.Context) error {
sa := machineControllerServiceAccount()
err := templates.EnsureServiceAccount(coreClient.ServiceAccounts(sa.Namespace), sa)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller service account")
}

// ClusterRoles
err = templates.EnsureClusterRole(rbacClient.ClusterRoles(), machineControllerClusterRole())
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller cluster role")
}

// ClusterRoleBindings
Expand All @@ -67,7 +67,7 @@ func Deploy(ctx *util.Context) error {
crbClient := rbacClient.ClusterRoleBindings()
for _, crbGen := range crbGenerators {
if err = templates.EnsureClusterRoleBinding(crbClient, crbGen()); err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller cluster-role binding")
}
}

Expand All @@ -82,7 +82,7 @@ func Deploy(ctx *util.Context) error {
for _, roleGen := range roleGenerators {
role := roleGen()
if err = templates.EnsureRole(rbacClient.Roles(role.Namespace), role); err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller role")
}
}

Expand All @@ -97,27 +97,27 @@ func Deploy(ctx *util.Context) error {
for _, roleBindingGen := range roleBindingsGenerators {
roleBinding := roleBindingGen()
if err = templates.EnsureRoleBinding(rbacClient.RoleBindings(roleBinding.Namespace), roleBinding); err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller role binding")
}
}

// Secrets
secret := machineControllerCredentialsSecret(ctx.Cluster)
err = templates.EnsureSecret(coreClient.Secrets(secret.Namespace), secret)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller credentials secret")
}

// Deployments
deployment, err := machineControllerDeployment(ctx.Cluster)
if err != nil {
return err
return errors.Wrap(err, "failed to generate machine-controller deployment")
}

deploymentClient := ctx.Clientset.AppsV1().Deployments(deployment.Namespace)
err = templates.EnsureDeployment(deploymentClient, deployment)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller deployment")
}

// CRDs
Expand All @@ -132,7 +132,7 @@ func Deploy(ctx *util.Context) error {
for _, crdGen := range crdGenerators {
err = templates.EnsureCRD(crdClient, crdGen())
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller CRDs")
}
}

Expand All @@ -146,7 +146,7 @@ func WaitForMachineController(corev1Client corev1types.CoreV1Interface) error {
LabelSelector: fmt.Sprintf("%s=%s", MachineControllerAppLabelKey, MachineControllerAppLabelValue),
})
if err != nil {
return false, err
return false, errors.Wrap(err, "failed to list machine-controller pod")
}

if len(machineControllerPods.Items) == 0 {
Expand Down Expand Up @@ -191,12 +191,6 @@ func machineControllerClusterRole() *rbacv1.ClusterRole {
Resources: []string{"customresourcedefinitions"},
Verbs: []string{"get"},
},
// {
// APIGroups: []string{"apiextensions.k8s.io"},
// Resources: []string{"customresourcedefinitions"},
// ResourceNames: []string{"machines.machine.k8s.io"},
// Verbs: []string{"delete"},
// },
{
APIGroups: []string{"apiextensions.k8s.io"},
Resources: []string{"customresourcedefinitions"},
Expand Down Expand Up @@ -235,8 +229,16 @@ func machineControllerClusterRole() *rbacv1.ClusterRole {
},
{
APIGroups: []string{"cluster.k8s.io"},
Resources: []string{"machines", "machinesets", "machinesets/status", "machinedeployments", "machinedeployments/status", "clusters", "clusters/status"},
Verbs: []string{"*"},
Resources: []string{
"clusters",
"clusters/status",
"machinedeployments",
"machinedeployments/status",
"machines",
"machinesets",
"machinesets/status",
},
Verbs: []string{"*"},
},
},
}
Expand Down Expand Up @@ -342,8 +344,8 @@ func machineControllerKubeSystemRole() *rbacv1.Role {
Resources: []string{"secrets"},
Verbs: []string{
"create",
"update",
"list",
"update",
"watch",
},
},
Expand Down Expand Up @@ -752,7 +754,7 @@ func machineControllerDeployment(cluster *config.Cluster) (*appsv1.Deployment, e

clusterDNS, err := clusterDNSIP(cluster)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get clusterDNS IP")
}

return &appsv1.Deployment{
Expand Down Expand Up @@ -896,13 +898,13 @@ func clusterDNSIP(cluster *config.Cluster) (*net.IP, error) {
// Get the Services CIDR
_, svcSubnetCIDR, err := net.ParseCIDR(cluster.Network.ServiceSubnet())
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to parse network.service_subnet")
}

// Select the 10th IP in Services CIDR range as ClusterDNSIP
clusterDNS, err := ipallocator.GetIndexedIP(svcSubnetCIDR, 10)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get IP from service subnet")
}

return &clusterDNS, nil
Expand Down
17 changes: 9 additions & 8 deletions pkg/templates/machinecontroller/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ func DeployMachineDeployments(ctx *util.Context) error {
// Create Cluster-API clientset
clusterapiClientset, err := clusterclientset.NewForConfig(ctx.RESTConfig)
if err != nil {
return err
return errors.Wrap(err, "failed to create kubernetes clientset")
}
clusterapiClient := clusterapiClientset.ClusterV1alpha1()

// Apply MachineDeployments
for _, workerset := range ctx.Cluster.Workers {
deployment, err := createMachineDeployment(ctx.Cluster, workerset)
if err != nil {
return err
return errors.Wrap(err, "failed to generate MachineDeployment")
}

err = ensureMachineDeployment(clusterapiClient.MachineDeployments(deployment.Namespace), deployment)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure MachineDeployment")
}
}

Expand All @@ -67,7 +67,7 @@ func createMachineDeployment(cluster *config.Cluster, workerset config.WorkerCon

cloudProviderSpec, err := machineSpec(cluster, workerset, provider)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to generate machineSpec")
}

config := providerSpec{
Expand All @@ -80,7 +80,7 @@ func createMachineDeployment(cluster *config.Cluster, workerset config.WorkerCon

encoded, err := json.Marshal(config)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to JSON marshal providerSpec")
}

replicas := int32(*workerset.Replicas)
Expand Down Expand Up @@ -139,10 +139,11 @@ func ensureMachineDeployment(machineDeploymentsClient clustertypes.MachineDeploy
existing, err := machineDeploymentsClient.Get(required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
_, err = machineDeploymentsClient.Create(required)
return err
return errors.Wrap(err, "failed to create MachineDeployment")
}

if err != nil {
return err
return errors.Wrap(err, "failed to get MachineDeployment")
}

modified := false
Expand All @@ -153,7 +154,7 @@ func ensureMachineDeployment(machineDeploymentsClient clustertypes.MachineDeploy
}

_, err = machineDeploymentsClient.Update(existing)
return err
return errors.Wrap(err, "failed to update MachineDeployment")
}

func machineSpec(cluster *config.Cluster, workerset config.WorkerConfig, provider config.ProviderName) (map[string]interface{}, error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/templates/machinecontroller/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,25 @@ func DeployWebhookConfiguration(ctx *util.Context) error {
deployment := webhookDeployment(ctx.Cluster)
err = templates.EnsureDeployment(appsClient.Deployments(deployment.Namespace), deployment)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller webhook deployment")
}

// Deploy Webhook service
svc := service()
err = templates.EnsureService(coreClient.Services(svc.Namespace), svc)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller webhook service")
}

// Deploy serving certificate secret
servingCert, err := tlsServingCertificate(caKeyPair)
if err != nil {
return err
return errors.Wrap(err, "failed to generate machine-controller webhook TLS secret")
}

err = templates.EnsureSecret(coreClient.Secrets(servingCert.Namespace), servingCert)
if err != nil {
return err
return errors.Wrap(err, "failed to ensure machine-controller webhook secret")
}

return templates.EnsureMutatingWebhookConfiguration(
Expand Down Expand Up @@ -268,6 +269,7 @@ func tlsServingCertificate(ca *triple.KeyPair) (*corev1.Secret, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to generate serving cert")
}

se.Data["cert.pem"] = certutil.EncodeCertPEM(newKP.Cert)
se.Data["key.pem"] = certutil.EncodePrivateKeyPEM(newKP.Key)
// Include the CA for simplicity
Expand Down

0 comments on commit 66818af

Please sign in to comment.