// CmdDelete implements 'delete' command func CmdDelete(c *cli.Context) { log.Debugf("deleting : %s", os.Getenv("KDEPLOY_KUBEWARE")) var kubewareName string var kubewareVersion string var labelSelector string var err error namespace := os.Getenv("KDEPLOY_NAMESPACE") kubeware := os.Getenv("KDEPLOY_KUBEWARE") localKubePath, err := fetchers.Fetch(kubeware) if err != nil { log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err)) } if localKubePath != "" { labelSelector, kubewareName, kubewareVersion = labelSelectorFromKubeware(localKubePath) } else { // could not be fetched so we will interpret it as a name kubewareName, err = utils.NormalizeName(kubeware) utils.CheckError(err) labelSelector = labelSelectorFromName(kubewareName) } kubernetes, err := webservice.NewKubeClient() utils.CheckError(err) // get services which are currently deployed as part of the kube serviceList, err := kubernetes.GetServicesForNamespace(namespace, labelSelector) utils.CheckError(err) log.Debugf("Services: %v", serviceList) // get controllers which are currently deployed as part of the kube controllerList, err := kubernetes.GetControllersForNamespace(namespace, labelSelector) utils.CheckError(err) log.Debugf("Controllers: %v", controllerList) // If no resources found that means it's not deployed if len(*serviceList) == 0 || len(*controllerList) == 0 { var version string if kubewareVersion != "" { version = fmt.Sprintf(" (%s)", kubewareVersion) } log.Warnf("Could not delete kubeware '%s'%s since it is not currently deployed", kubewareName, version) return } // delete them ds := deletionStrategies.WaitZeroReplicasDeletionStrategy(kubernetes) err = ds.Delete(namespace, svcNames(serviceList), rcNames(controllerList)) utils.CheckError(err) log.Infof("Kubeware '%s.%s' has been deleted", namespace, kubewareName) }
func onlyMetadata(svcJSON string) string { var svc map[string]interface{} err := json.Unmarshal([]byte(svcJSON), &svc) utils.CheckError(err) filtered := map[string]interface{}{ "apiVersion": svc["apiVersion"], "kind": svc["kind"], "metadata": svc["metadata"], } filteredJSON, err := json.Marshal(filtered) utils.CheckError(err) return string(filteredJSON) }
// ParseMetadata parses the metadata file in the kube dir func ParseMetadata(path string) Metadata { absPath, err := filepath.Abs(path) utils.CheckError(err) metadataFile := fmt.Sprintf("%s/metadata.yaml", filepath.Clean(absPath)) utils.CheckError(err) metadataContent, err := ioutil.ReadFile(metadataFile) utils.CheckError(err) var metadata Metadata err = yaml.Unmarshal(metadataContent, &metadata) utils.CheckError(err) metadata.path = filepath.Dir(metadataFile) return metadata }
func labelSelectorFromKubeware(localKubePath string) (string, string, string) { md := template.ParseMetadata(localKubePath) normalizedName, err := utils.NormalizeName(md.Name) utils.CheckError(err) labelSelector := fmt.Sprintf("kubeware=%s,kubeware-version=%s", normalizedName, md.Version) return labelSelector, md.Name, md.Version }
func jsonPatchSpecReplicas(nr uint) []byte { var patch struct { Spec struct { Replicas uint `json:"replicas"` } `json:"spec"` } patch.Spec.Replicas = nr data, err := json.Marshal(patch) utils.CheckError(err) return data }
// CmdShow implements the 'show' command func CmdShow(c *cli.Context) { utils.CheckRequiredFlags(c, []string{"kubeware"}) kubeware := os.Getenv("KDEPLOY_KUBEWARE") localKubePath, err := fetchers.Fetch(kubeware) if err != nil { log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err)) } log.Debugf("Going to parse kubeware in %s", localKubePath) metadata := template.ParseMetadata(localKubePath) defaults, err := metadata.AttributeDefaults() utils.CheckError(err) log.Debugf("Building attributes") attributes := template.BuildAttributes(os.Getenv("KDEPLOY_ATTRIBUTE"), defaults) log.Debugf("Parsing services") servicesSpecs, err := metadata.ParseServices(attributes) utils.CheckError(err) log.Debugf("Parsing controllers") controllersSpecs, err := metadata.ParseControllers(attributes) utils.CheckError(err) // print resolved resources for _, s := range servicesSpecs { y, err := gyml.JSONToYAML([]byte(s)) utils.CheckError(err) fmt.Println(string(y)) } for _, c := range controllersSpecs { y, err := gyml.JSONToYAML([]byte(c)) utils.CheckError(err) fmt.Println(string(y)) } }
// CmdDeploy implements the 'deploy' command func CmdDeploy(c *cli.Context) { utils.CheckRequiredFlags(c, []string{"kubeware"}) var kubeware string var localKubePath string var err error kubeware = os.Getenv("KDEPLOY_KUBEWARE") localKubePath, err = fetchers.Fetch(kubeware) if err != nil { log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err)) } log.Debugf("Going to parse kubeware in %s", localKubePath) metadata := template.ParseMetadata(localKubePath) defaults, err := metadata.AttributeDefaults() utils.CheckError(err) // build attributes merging "role list" to defaults log.Debugf("Building attributes") attributes := template.BuildAttributes(c.String("attribute"), defaults) // get list of services and parse each one log.Debugf("Parsing services") servicesSpecs, err := metadata.ParseServices(attributes) utils.CheckError(err) // get list of replica controllers and parse each one log.Debugf("Parsing controllers") controllersSpecs, err := metadata.ParseControllers(attributes) utils.CheckError(err) // creates Kubernetes client kubernetes, err := webservice.NewKubeClient() utils.CheckError(err) // check if kubeware already exists log.Debugf("Checking if already deployed") deployedVersion, err := kubernetes.FindDeployedKubewareVersion(os.Getenv("KDEPLOY_NAMESPACE"), metadata.Name) utils.CheckError(err) if deployedVersion != "" { log.Errorf("Can not deploy '%s' since version '%s' is already deployed", metadata.Name, deployedVersion) return } // create each of the services log.Debugf("Creating services") err = kubernetes.CreateServices(utils.Values(servicesSpecs)) utils.CheckError(err) // create each of the controllers log.Debugf("Creating controllers") err = kubernetes.CreateReplicaControllers(utils.Values(controllersSpecs)) utils.CheckError(err) log.Infof("Kubeware %s from %s has been deployed", metadata.Name, os.Getenv("KDEPLOY_KUBEWARE")) }
func BuildAttributes(filePath string, defaults map[string]interface{}) map[string]interface{} { // just defaults if no attributes given if filePath == "" { return defaults } attJSON, err := ioutil.ReadFile(filePath) if err != nil { log.Fatalf("could not read file '%s' (%v)", filePath, err) } var attribs map[string]interface{} err = json.Unmarshal(attJSON, &attribs) utils.CheckError(err) attributes := deeply.Merge(defaults, attribs) return attributes }
// CmdUpgrade implements 'upgrade' command func CmdUpgrade(c *cli.Context) { var kubeware string var localKubePath string var err error kubeware = os.Getenv("KDEPLOY_KUBEWARE") localKubePath, err = fetchers.Fetch(kubeware) if err != nil { log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err)) } log.Debugf("Going to parse kubeware in %s", localKubePath) md := template.ParseMetadata(localKubePath) utils.CheckError(err) kubernetes, err := webservice.NewKubeClient() utils.CheckError(err) namespace := os.Getenv("KDEPLOY_NAMESPACE") // labelSelector := fmt.Sprintf("kubeware=%s,kubeware-version=%s", md.Name, md.Version) // Check if kubeware already installed, error if it's not v, err := kubernetes.FindDeployedKubewareVersion(namespace, md.Name) utils.CheckError(err) if v == "" { log.Fatalf("Kubeware '%s.%s' is not deployed and thus it can't be upgraded", namespace, md.Name) } log.Infof("Found version %s of %s.%s", v, namespace, md.Name) // Check if equal or newer version already exists, error if so deployedVersion, err := version.NewVersion(v) utils.CheckError(err) upgradeVersion, err := version.NewVersion(md.Version) utils.CheckError(err) if upgradeVersion.LessThan(deployedVersion) { log.Fatalf("Can not upgrade to version '%s' since version '%s' is already deployed", md.Version, v) } // build attributes merging "role list" to defaults log.Debugf("Building attributes") defaults, err := md.AttributeDefaults() utils.CheckError(err) attributes := template.BuildAttributes(c.String("attribute"), defaults) // get services and parse each one log.Debugf("Parsing services") servicesSpecs, err := md.ParseServices(attributes) utils.CheckError(err) // get replica controllers and parse each one log.Debugf("Parsing controllers") controllersSpecs, err := md.ParseControllers(attributes) utils.CheckError(err) // upgStrategy := upgradeStrategies.RecreateAllStrategy(kubernetes) // upgStrategy := upgradeStrategies.RollRcPatchSvcStrategy(kubernetes, 1) upgStrategy := upgradeStrategies.BuildUpgradeStrategy(os.Getenv("KDEPLOY_UPGRADE_STRATEGY"), kubernetes) upgStrategy.Upgrade(namespace, servicesSpecs, controllersSpecs) log.Infof("Kubeware '%s.%s' has been upgraded from version '%s' to '%s'", namespace, md.Name, v, md.Version) }
// CmdList implements 'list' command func CmdList(c *cli.Context) { var fqdns []string up := 0 kubernetes, err := webservice.NewKubeClient() utils.CheckError(err) // Get all services to extract their kubeware labels log.Debug("Get all services to extract their kubeware labels ...") serviceList, err := kubernetes.GetServices() utils.CheckError(err) // Get all controllers to extract their kubeware labels log.Debug("Get all controllers to extract their kubeware labels ...") controllersList, err := kubernetes.GetControllers() utils.CheckError(err) // build the list to be printed kubeList := models.BuildKubeList(serviceList, controllersList) if len(kubeList) > 0 { w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0) if c.Bool("all") || (!c.Bool("services") && !c.Bool("controllers")) { fmt.Fprintln(w, "KUBEWARE\tNAMESPACE\tVERSION\tSVC\tRC\tUP\tFQDN\r") for _, kubeware := range kubeList { for _, service := range kubeware.Services { if service.GetFQDN() != "" { fqdns = append(fqdns, service.GetFQDN()) } } for _, controller := range kubeware.ReplicaControllers { up = up + controller.GetUpStats() } if len(kubeware.Services) > 0 { up = up / len(kubeware.Services) } if len(fqdns) > 0 { fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d\t%d%%\t%s\n", kubeware.GetKube(), kubeware.GetNamespace(), kubeware.GetVersion(), len(kubeware.Services), len(kubeware.ReplicaControllers), up, strings.Join(fqdns, ",")) } else { fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d%%\t%d\n", kubeware.GetKube(), kubeware.GetNamespace(), kubeware.GetVersion(), len(kubeware.Services), up, len(kubeware.ReplicaControllers)) } up = 0 fqdns = []string{} } } if c.Bool("all") { fmt.Fprintf(w, "\n") } if c.Bool("all") || c.Bool("services") { fmt.Fprintln(w, "KUBEWARE\tNAMESPACE\tSVC\tINTERNAL IP\tFQDN\r") for _, kubeware := range kubeList { for _, service := range kubeware.Services { if service.GetFQDN() != "" { fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", kubeware.GetKube(), service.GetNamespace(), service.GetName(), service.GetInternalIp(), service.GetFQDN()) } else { fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", kubeware.GetKube(), service.GetNamespace(), service.GetName(), service.GetInternalIp()) } } } } if c.Bool("all") { fmt.Fprintf(w, "\n") } if c.Bool("all") || c.Bool("controllers") { fmt.Fprintln(w, "KUBEWARE\tNAMESPACE\tRC\tREPLICAS\tUP\r") for _, kubeware := range kubeList { for _, replicaController := range kubeware.ReplicaControllers { fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d%%\n", kubeware.GetKube(), kubeware.GetNamespace(), replicaController.GetName(), replicaController.GetReplicas(), replicaController.GetUpStats()) } } } w.Flush() } else { log.Infof("No Kubeware deployed") } }