Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Upgrade Command #144

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For more information including a complete list of litmusctl operations, see the
* For v0.12.0 or latest:
* Non-Interactive mode: <a href="https://github.com/litmuschaos/litmusctl/blob/master/Usage.md">Click here</a>
* Interactive mode: <a href="https://github.com/litmuschaos/litmusctl/blob/master/Usage_interactive.md">Click here</a>
* For 0.23.0: <a href="https://github.com/litmuschaos/litmusctl/blob/master/Usage_0.23.0.md">Click here</a>
* For v0.2.0 or earlier && compatible with Litmus-2.0.0-Beta8 or earlier: <a href="https://github.com/litmuschaos/litmusctl/blob/master/Usage_v0.2.0.md">Click here</a>

## Requirements
Expand Down
40 changes: 20 additions & 20 deletions pkg/apis/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ type manifestData struct {
}

type data struct {
GetManifest string `json:"getManifest"`
GetManifest string `json:"getInfraManifest"`
}

type InfrasData struct {
Data GetInfraDetails `json:"data"`
type GetInfraResponse struct {
Data GetInfraData `json:"data"`
Errors []struct {
Message string `json:"message"`
Path []string `json:"path"`
} `json:"errors"`
}

type GetInfraDetails struct {
GetInfraDetails InfrasDetails `json:"getAgentDetails"`
type GetInfraData struct {
GetInfraDetails InfraDetails `json:"getInfraDetails"`
}

type InfrasDetails struct {
type InfraDetails struct {
InfraID string `json:"infraID"`
InfraNamespace *string `json:"infraNamespace"`
}

func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, infraID string, kubeconfig string) (string, error) {

// Query to fetch agent details from server
// Query to fetch Infra details from server
query := `{"query":"query {\n getInfraDetails(infraID : \"` + infraID + `\", \n projectID : \"` + projectID + `\"){\n infraNamespace infraID \n}}"}`
resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(query), string(types.Post))
if err != nil {
Expand All @@ -61,7 +61,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
}

defer resp.Body.Close()
var infra InfrasData
var infra GetInfraResponse

if resp.StatusCode == http.StatusOK {
err = json.Unmarshal(bodyBytes, &infra)
Expand Down Expand Up @@ -102,27 +102,27 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
}

// To write the manifest data into a temporary file
err = ioutil.WriteFile("chaos-delegate-manifest.yaml", []byte(manifest.Data.GetManifest), 0644)
err = ioutil.WriteFile("chaos-infra-manifest.yaml", []byte(manifest.Data.GetManifest), 0644)
if err != nil {
return "", err
}

// Fetching agent-config from the subscriber
configData, err := k8s.GetConfigMap(c, "agent-config", *infra.Data.GetInfraDetails.InfraNamespace)
// Fetching subscriber-config from the subscriber
configData, err := k8s.GetConfigMap(c, "subscriber-config", *infra.Data.GetInfraDetails.InfraNamespace)
if err != nil {
return "", err
}
var configMapString string

metadata := new(bytes.Buffer)
fmt.Fprintf(metadata, "\n%s: %s\n%s: %s\n%s: \n %s: %s\n %s: %s\n%s:\n", "apiVersion", "v1",
"kind", "ConfigMap", "metadata", "name", "agent-config", "namespace", *infra.Data.GetInfraDetails.InfraNamespace, "data")
"kind", "ConfigMap", "metadata", "name", "subscriber-config", "namespace", *infra.Data.GetInfraDetails.InfraNamespace, "data")

for k, v := range configData {
b := new(bytes.Buffer)
if k == "COMPONENTS" {
fmt.Fprintf(b, " %s: |\n %s", k, v)
} else if k == "START_TIME" || k == "IS_CLUSTER_CONFIRMED" {
} else if k == "START_TIME" || k == "IS_INFRA_CONFIRMED" {
fmt.Fprintf(b, " %s: \"%s\"\n", k, v)
} else {
fmt.Fprintf(b, " %s: %s\n", k, v)
Expand All @@ -134,30 +134,30 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
yamlOutput, err := k8s.ApplyYaml(k8s.ApplyYamlPrams{
Token: cred.Token,
Endpoint: cred.Endpoint,
YamlPath: "chaos-delegate-manifest.yaml",
YamlPath: "chaos-infra-manifest.yaml",
}, kubeconfig, true)

if err != nil {
return "", err
}
utils.White.Print("\n", yamlOutput)

err = os.Remove("chaos-delegate-manifest.yaml")
err = os.Remove("chaos-infra-manifest.yaml")
if err != nil {
return "Error removing Chaos Delegate manifest: ", err
return "Error removing Chaos Infrastructure manifest: ", err
}

// Creating a backup for current agent-config in the SUBSCRIBER
// Creating a backup for current subscriber-config in the SUBSCRIBER
home, err := homedir.Dir()
cobra.CheckErr(err)

configMapString = metadata.String() + configMapString
err = ioutil.WriteFile(home+"/backupAgentConfig.yaml", []byte(configMapString), 0644)
err = ioutil.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644)
if err != nil {
return "Error creating backup for agent config: ", err
return "Error creating backup for subscriber config: ", err
}

utils.White_B.Print("\n ** A backup of agent-config configmap has been saved in your system's home directory as backupAgentConfig.yaml **\n")
utils.White_B.Print("\n ** A backup of subscriber-config configmap has been saved in your system's home directory as backupSubscriberConfig.yaml **\n")

return "Manifest applied successfully", nil
} else {
Expand Down
24 changes: 12 additions & 12 deletions pkg/cmd/connect/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"os"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/infra_ops"
"github.com/litmuschaos/litmusctl/pkg/k8s"
"github.com/litmuschaos/litmusctl/pkg/ops"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"

Expand Down Expand Up @@ -79,7 +79,7 @@ var infraCmd = &cobra.Command{

if !projectExists {
utils.White_B.Print("Creating a random project...")
newInfra.ProjectId = ops.CreateRandomProject(credentials)
newInfra.ProjectId = infra_ops.CreateRandomProject(credentials)
}
}

Expand Down Expand Up @@ -166,14 +166,14 @@ var infraCmd = &cobra.Command{

// Check if user has sufficient permissions based on mode
utils.White_B.Print("\n🏃 Running prerequisites check....")
ops.ValidateSAPermissions(newInfra.Namespace, newInfra.Mode, &kubeconfig)
infra_ops.ValidateSAPermissions(newInfra.Namespace, newInfra.Mode, &kubeconfig)

// Check if infra already exists
isInfraExist, err, infraList := ops.ValidateInfraNameExists(newInfra.InfraName, newInfra.ProjectId, credentials)
isInfraExist, err, infraList := infra_ops.ValidateInfraNameExists(newInfra.InfraName, newInfra.ProjectId, credentials)
utils.PrintError(err)

if isInfraExist {
ops.PrintExistingInfra(infraList)
infra_ops.PrintExistingInfra(infraList)
os.Exit(1)
}
envIDs, err := environment.GetEnvironmentList(newInfra.ProjectId, credentials)
Expand All @@ -190,7 +190,7 @@ var infraCmd = &cobra.Command{
}
if !isEnvExist {
utils.Red.Println("\nChaos Environment with the given ID doesn't exists.")
ops.PrintExistingEnvironments(envIDs)
infra_ops.PrintExistingEnvironments(envIDs)
utils.White_B.Println("\n❗ Please enter a name from the List or Create a new environment using `litmusctl create chaos-environment`")
os.Exit(1)
}
Expand All @@ -201,25 +201,25 @@ var infraCmd = &cobra.Command{

if newInfra.ProjectId == "" {
// Fetch project id
newInfra.ProjectId = ops.GetProjectID(userDetails)
newInfra.ProjectId = infra_ops.GetProjectID(userDetails)
}

modeType := ops.GetModeType()
modeType := infra_ops.GetModeType()

// Check if user has sufficient permissions based on mode
utils.White_B.Print("\n🏃 Running prerequisites check....")
ops.ValidateSAPermissions(newInfra.Namespace, modeType, &kubeconfig)
newInfra, err = ops.GetInfraDetails(modeType, newInfra.ProjectId, credentials, &kubeconfig)
infra_ops.ValidateSAPermissions(newInfra.Namespace, modeType, &kubeconfig)
newInfra, err = infra_ops.GetInfraDetails(modeType, newInfra.ProjectId, credentials, &kubeconfig)
utils.PrintError(err)

newInfra.ServiceAccount, newInfra.SAExists = k8s.ValidSA(newInfra.Namespace, &kubeconfig)
newInfra.Mode = modeType
}

ops.Summary(newInfra, &kubeconfig)
infra_ops.Summary(newInfra, &kubeconfig)

if !nonInteractive {
ops.ConfirmInstallation()
infra_ops.ConfirmInstallation()
}

infra, err := infrastructure.ConnectInfra(newInfra, credentials)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/create/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
"github.com/litmuschaos/litmusctl/pkg/ops"
"github.com/litmuschaos/litmusctl/pkg/infra_ops"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
"os"
Expand Down Expand Up @@ -109,7 +109,7 @@ var environmentCmd = &cobra.Command{
}
if isEnvExist {
utils.Red.Println("\nChaos Environment with the given ID already exists, try with a different name")
ops.PrintExistingEnvironments(envs)
infra_ops.PrintExistingEnvironments(envs)
os.Exit(1)
}

Expand Down
31 changes: 18 additions & 13 deletions pkg/cmd/upgrade/agent.go → pkg/cmd/upgrade/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var agentCmd = &cobra.Command{
Use: "chaos-delegate",
Short: `Upgrades the LitmusChaos agent plane.`,
var infraCmd = &cobra.Command{
Use: "chaos-infra",
Short: `Upgrades the LitmusChaos Execution plane.`,
Run: func(cmd *cobra.Command, args []string) {
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)
Expand All @@ -41,29 +42,33 @@ var agentCmd = &cobra.Command{
fmt.Scanln(&projectID)
}

cluster_id, err := cmd.Flags().GetString("chaos-delegate-id")
infraID, err := cmd.Flags().GetString("chaos-infra-id")
utils.PrintError(err)

if cluster_id == "" {
utils.White_B.Print("\nEnter the Chaos Delegate ID: ")
fmt.Scanln(&cluster_id)
if infraID == "" {
utils.White_B.Print("\nEnter the Chaos Infra ID: ")
fmt.Scanln(&infraID)
}

kubeconfig, err := cmd.Flags().GetString("kubeconfig")
utils.PrintError(err)

output, err := apis.UpgradeInfra(context.Background(), credentials, projectID, cluster_id, kubeconfig)
output, err := apis.UpgradeInfra(context.Background(), credentials, projectID, infraID, kubeconfig)
if err != nil {
utils.Red.Print("\n❌ Failed upgrading Chaos Delegate: \n" + err.Error() + "\n")
if strings.Contains(err.Error(), "no documents in result") {
utils.Red.Println("❌ The specified Project ID or Chaos Infrastructure ID doesn't exist.")
os.Exit(1)
}
utils.Red.Print("\n❌ Failed upgrading Chaos Infrastructure: \n" + err.Error() + "\n")
os.Exit(1)
}
utils.White_B.Print("\n", output)
},
}

func init() {
UpgradeCmd.AddCommand(agentCmd)
agentCmd.Flags().String("project-id", "", "Enter the project ID")
agentCmd.Flags().String("kubeconfig", "", "Enter the kubeconfig path(default: $HOME/.kube/config))")
agentCmd.Flags().String("chaos-delegate-id", "", "Enter the Chaos Delegate ID")
UpgradeCmd.AddCommand(infraCmd)
infraCmd.Flags().String("project-id", "", "Enter the project ID")
infraCmd.Flags().String("kubeconfig", "", "Enter the kubeconfig path(default: $HOME/.kube/config))")
infraCmd.Flags().String("chaos-infra-id", "", "Enter the Chaos Infrastructure ID")
}
4 changes: 2 additions & 2 deletions pkg/cmd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
var UpgradeCmd = &cobra.Command{
Use: "upgrade",
Short: `Examples:
#upgrade version of your Chaos Delegate
litmusctl upgrade chaos-delegate --chaos-delegate-id="4cc25543-36c8-4373-897b-2e5dbbe87bcf" --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --non-interactive
#upgrade version of your Chaos Infrastructure
litmusctl upgrade chaos-infra --chaos-infra-id="4cc25543-36c8-4373-897b-2e5dbbe87bcf" --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --non-interactive

Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ops/ops.go → pkg/infra_ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ops
package infra_ops

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/ops/platform.go → pkg/infra_ops/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ops
package infra_ops

import (
"context"
Expand Down
File renamed without changes.
Loading