Exemple #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
}
Exemple #2
0
// 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"))
}
Exemple #3
0
// 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))
	}
}
Exemple #4
0
// 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)
}