Skip to content

Commit

Permalink
Add context.Context to everything and also make logging pluggable
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jan 9, 2018
1 parent 6dbb6cb commit d8dd297
Show file tree
Hide file tree
Showing 30 changed files with 448 additions and 365 deletions.
16 changes: 9 additions & 7 deletions authz/authz.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package authz

import (
"context"

"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/rke/templates"
"github.com/sirupsen/logrus"
)

func ApplyJobDeployerServiceAccount(kubeConfigPath string) error {
logrus.Infof("[authz] Creating rke-job-deployer ServiceAccount")
func ApplyJobDeployerServiceAccount(ctx context.Context, kubeConfigPath string) error {
log.Infof(ctx, "[authz] Creating rke-job-deployer ServiceAccount")
k8sClient, err := k8s.NewClient(kubeConfigPath)
if err != nil {
return err
Expand All @@ -18,19 +20,19 @@ func ApplyJobDeployerServiceAccount(kubeConfigPath string) error {
if err := k8s.UpdateServiceAccountFromYaml(k8sClient, templates.JobDeployerServiceAccount); err != nil {
return err
}
logrus.Infof("[authz] rke-job-deployer ServiceAccount created successfully")
log.Infof(ctx, "[authz] rke-job-deployer ServiceAccount created successfully")
return nil
}

func ApplySystemNodeClusterRoleBinding(kubeConfigPath string) error {
logrus.Infof("[authz] Creating system:node ClusterRoleBinding")
func ApplySystemNodeClusterRoleBinding(ctx context.Context, kubeConfigPath string) error {
log.Infof(ctx, "[authz] Creating system:node ClusterRoleBinding")
k8sClient, err := k8s.NewClient(kubeConfigPath)
if err != nil {
return err
}
if err := k8s.UpdateClusterRoleBindingFromYaml(k8sClient, templates.SystemNodeClusterRoleBinding); err != nil {
return err
}
logrus.Infof("[authz] system:node ClusterRoleBinding created successfully")
log.Infof(ctx, "[authz] system:node ClusterRoleBinding created successfully")
return nil
}
16 changes: 9 additions & 7 deletions authz/psp.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package authz

import (
"context"

"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/rke/templates"
"github.com/sirupsen/logrus"
)

func ApplyDefaultPodSecurityPolicy(kubeConfigPath string) error {
logrus.Infof("[authz] Applying default PodSecurityPolicy")
func ApplyDefaultPodSecurityPolicy(ctx context.Context, kubeConfigPath string) error {
log.Infof(ctx, "[authz] Applying default PodSecurityPolicy")
k8sClient, err := k8s.NewClient(kubeConfigPath)
if err != nil {
return err
}
if err := k8s.UpdatePodSecurityPolicyFromYaml(k8sClient, templates.DefaultPodSecurityPolicy); err != nil {
return err
}
logrus.Infof("[authz] Default PodSecurityPolicy applied successfully")
log.Infof(ctx, "[authz] Default PodSecurityPolicy applied successfully")
return nil
}

func ApplyDefaultPodSecurityPolicyRole(kubeConfigPath string) error {
logrus.Infof("[authz] Applying default PodSecurityPolicy Role and RoleBinding")
func ApplyDefaultPodSecurityPolicyRole(ctx context.Context, kubeConfigPath string) error {
log.Infof(ctx, "[authz] Applying default PodSecurityPolicy Role and RoleBinding")
k8sClient, err := k8s.NewClient(kubeConfigPath)
if err != nil {
return err
Expand All @@ -31,6 +33,6 @@ func ApplyDefaultPodSecurityPolicyRole(kubeConfigPath string) error {
if err := k8s.UpdateRoleBindingFromYaml(k8sClient, templates.DefaultPodSecurityRoleBinding); err != nil {
return err
}
logrus.Infof("[authz] Default PodSecurityPolicy Role and RoleBinding applied successfully")
log.Infof(ctx, "[authz] Default PodSecurityPolicy Role and RoleBinding applied successfully")
return nil
}
37 changes: 19 additions & 18 deletions cluster/addons.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package cluster

import (
"context"
"fmt"
"time"

"github.com/rancher/rke/addons"
"github.com/rancher/rke/k8s"
"github.com/sirupsen/logrus"
"github.com/rancher/rke/log"
)

const (
KubeDNSAddonResourceName = "rke-kubedns-addon"
UserAddonResourceName = "rke-user-addon"
)

func (c *Cluster) DeployK8sAddOns() error {
err := c.deployKubeDNS()
func (c *Cluster) DeployK8sAddOns(ctx context.Context) error {
err := c.deployKubeDNS(ctx)
return err
}

func (c *Cluster) DeployUserAddOns() error {
logrus.Infof("[addons] Setting up user addons..")
func (c *Cluster) DeployUserAddOns(ctx context.Context) error {
log.Infof(ctx, "[addons] Setting up user addons..")
if c.Addons == "" {
logrus.Infof("[addons] No user addons configured..")
log.Infof(ctx, "[addons] No user addons configured..")
return nil
}

if err := c.doAddonDeploy(c.Addons, UserAddonResourceName); err != nil {
if err := c.doAddonDeploy(ctx, c.Addons, UserAddonResourceName); err != nil {
return err
}
logrus.Infof("[addons] User addon deployed successfully..")
log.Infof(ctx, "[addons] User addon deployed successfully..")
return nil

}

func (c *Cluster) deployKubeDNS() error {
logrus.Infof("[addons] Setting up KubeDNS")
func (c *Cluster) deployKubeDNS(ctx context.Context) error {
log.Infof(ctx, "[addons] Setting up KubeDNS")
kubeDNSConfig := map[string]string{
addons.KubeDNSServer: c.ClusterDNSServer,
addons.KubeDNSClusterDomain: c.ClusterDomain,
Expand All @@ -48,22 +49,22 @@ func (c *Cluster) deployKubeDNS() error {
if err != nil {
return err
}
if err := c.doAddonDeploy(kubeDNSYaml, KubeDNSAddonResourceName); err != nil {
if err := c.doAddonDeploy(ctx, kubeDNSYaml, KubeDNSAddonResourceName); err != nil {
return err
}
logrus.Infof("[addons] KubeDNS deployed successfully..")
log.Infof(ctx, "[addons] KubeDNS deployed successfully..")
return nil

}

func (c *Cluster) doAddonDeploy(addonYaml, resourceName string) error {
func (c *Cluster) doAddonDeploy(ctx context.Context, addonYaml, resourceName string) error {

err := c.StoreAddonConfigMap(addonYaml, resourceName)
err := c.StoreAddonConfigMap(ctx, addonYaml, resourceName)
if err != nil {
return fmt.Errorf("Failed to save addon ConfigMap: %v", err)
}

logrus.Infof("[addons] Executing deploy job..")
log.Infof(ctx, "[addons] Executing deploy job..")

addonJob, err := addons.GetAddonsExcuteJob(resourceName, c.ControlPlaneHosts[0].HostnameOverride, c.Services.KubeAPI.Image)
if err != nil {
Expand All @@ -76,8 +77,8 @@ func (c *Cluster) doAddonDeploy(addonYaml, resourceName string) error {
return nil
}

func (c *Cluster) StoreAddonConfigMap(addonYaml string, addonName string) error {
logrus.Infof("[addons] Saving addon ConfigMap to Kubernetes")
func (c *Cluster) StoreAddonConfigMap(ctx context.Context, addonYaml string, addonName string) error {
log.Infof(ctx, "[addons] Saving addon ConfigMap to Kubernetes")
kubeClient, err := k8s.NewClient(c.LocalKubeConfigPath)
if err != nil {
return err
Expand All @@ -91,7 +92,7 @@ func (c *Cluster) StoreAddonConfigMap(addonYaml string, addonName string) error
fmt.Println(err)
continue
}
logrus.Infof("[addons] Successfully Saved addon to Kubernetes ConfigMap: %s", addonName)
log.Infof(ctx, "[addons] Successfully Saved addon to Kubernetes ConfigMap: %s", addonName)
timeout <- true
break
}
Expand Down
18 changes: 10 additions & 8 deletions cluster/certificates.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package cluster

import (
"context"
"crypto/rsa"
"fmt"
"time"

"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/rke/pki"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/cert"
)

func SetUpAuthentication(kubeCluster, currentCluster *Cluster) error {
func SetUpAuthentication(ctx context.Context, kubeCluster, currentCluster *Cluster) error {
if kubeCluster.Authentication.Strategy == X509AuthenticationProvider {
var err error
if currentCluster != nil {
kubeCluster.Certificates = currentCluster.Certificates
} else {
kubeCluster.Certificates, err = pki.StartCertificatesGeneration(
kubeCluster.Certificates, err = pki.StartCertificatesGeneration(ctx,
kubeCluster.ControlPlaneHosts,
kubeCluster.WorkerHosts,
kubeCluster.ClusterDomain,
Expand Down Expand Up @@ -53,8 +55,8 @@ func regenerateAPICertificate(c *Cluster, certificates map[string]pki.Certificat
return certificates, nil
}

func getClusterCerts(kubeClient *kubernetes.Clientset) (map[string]pki.CertificatePKI, error) {
logrus.Infof("[certificates] Getting Cluster certificates from Kubernetes")
func getClusterCerts(ctx context.Context, kubeClient *kubernetes.Clientset) (map[string]pki.CertificatePKI, error) {
log.Infof(ctx, "[certificates] Getting Cluster certificates from Kubernetes")
certificatesNames := []string{
pki.CACertName,
pki.KubeAPICertName,
Expand Down Expand Up @@ -82,19 +84,19 @@ func getClusterCerts(kubeClient *kubernetes.Clientset) (map[string]pki.Certifica
KeyEnvName: string(secret.Data["KeyEnvName"]),
}
}
logrus.Infof("[certificates] Successfully fetched Cluster certificates from Kubernetes")
log.Infof(ctx, "[certificates] Successfully fetched Cluster certificates from Kubernetes")
return certMap, nil
}

func saveClusterCerts(kubeClient *kubernetes.Clientset, crts map[string]pki.CertificatePKI) error {
logrus.Infof("[certificates] Save kubernetes certificates as secrets")
func saveClusterCerts(ctx context.Context, kubeClient *kubernetes.Clientset, crts map[string]pki.CertificatePKI) error {
log.Infof(ctx, "[certificates] Save kubernetes certificates as secrets")
for crtName, crt := range crts {
err := saveCertToKubernetes(kubeClient, crtName, crt)
if err != nil {
return fmt.Errorf("Failed to save certificate [%s] to kubernetes: %v", crtName, err)
}
}
logrus.Infof("[certificates] Successfully saved certificates as kubernetes secret [%s]", pki.CertificatesSecretName)
log.Infof(ctx, "[certificates] Successfully saved certificates as kubernetes secret [%s]", pki.CertificatesSecretName)
return nil
}

Expand Down
Loading

0 comments on commit d8dd297

Please sign in to comment.