Beispiel #1
0
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
}
Beispiel #2
0
// 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)
}
Beispiel #3
0
func (k *kubeClient) FindDeployedKubewareVersion(namespace, name string) (string, error) {
	kubename, err := utils.NormalizeName(name)
	if err != nil {
		return "", err
	}
	versions := map[string]string{}
	services, err := k.GetServicesForNamespace(namespace)
	if err != nil {
		return "", err
	}
	controllers, err := k.GetControllersForNamespace(namespace)
	if err != nil {
		return "", err
	}
	// Iterate over services and collect versions
	for _, s := range *services {
		n := s.GetKube()
		v := s.GetVersion()
		prev, found := versions[n]
		// Check if version already found
		if !found {
			versions[n] = v
		}
		// Check if a different version was already found
		if found && prev != v {
			return "", fmt.Errorf("found more than one version of the same Kubeware (%s.%s %s/%s)", namespace, kubename, prev, v)
		}
	}
	// Iterate over controllers and collect versions
	for _, c := range *controllers {
		n := c.GetKube()
		v := c.GetVersion()
		prev, found := versions[n]
		// Check if version already found
		if !found {
			versions[n] = v
		}
		// Check if a different version was already found
		if found && prev != v {
			return "", fmt.Errorf("found more than one version of the same Kubeware (%s.%s %s/%s)", namespace, kubename, prev, v)
		}
	}
	v, found := versions[kubename]
	if !found {
		return "", nil
	}
	return v, nil
}
Beispiel #4
0
func addKubewareLabel(name, version string, specmap map[string]interface{}) error {
	fixedName, err := utils.NormalizeName(name)
	if err != nil {
		return err
	}
	metadata := specmap["metadata"].(map[string]interface{})
	if metadata["labels"] != nil {
		labels := metadata["labels"].(map[string]interface{})
		labels["kubeware"] = fixedName
		labels["kubeware-version"] = version
	} else {
		metadata["labels"] = map[string]interface{}{
			"kubeware":         fixedName,
			"kubeware-version": version,
		}
	}

	return nil
}