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

Create validate command #75

Merged
merged 16 commits into from
Nov 10, 2023
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ var rootCmd = &cobra.Command{
// Execute the top level command
func Execute() {
if err := rootCmd.Execute(); err != nil {
// fmt.Println(radixCLIError)
// fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand Down
35 changes: 35 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © 2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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 cmd

import (
"errors"

"github.com/spf13/cobra"
)

// validateCmd represents the validate command
var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate Radix resources",
Long: `Validate Radix resources.`,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("please specify the resource you want to validate")
},
}

func init() {
rootCmd.AddCommand(validateCmd)
}
89 changes: 89 additions & 0 deletions cmd/validateRadixConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright © 2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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 cmd

import (
"fmt"
"os"

radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/radixvalidators"
"github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

// logoutCmd represents the logout command
var validateRadixConfigCmd = &cobra.Command{
Use: "radix-config",
Short: "Validate radixconfig.yaml",
Long: `Check radixconfig.yaml for structural and logical errors`,
RunE: func(cmd *cobra.Command, args []string) error {

cmd.SilenceUsage = true

radixconfig, err := cmd.Flags().GetString("config-file")
if err != nil {
return err
}

printfile, err := cmd.Flags().GetBool("print")
if err != nil {
return err
}

if _, err := os.Stat(radixconfig); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("RadixConfig file not found: %s", radixconfig)
}

ra, err := utils.GetRadixApplicationFromFile(radixconfig)
if err != nil {
return fmt.Errorf("RadixConfig is invalid: %w", err)
}

if printfile {
err = printRA(ra)
if err != nil {
return err
}
}

err = radixvalidators.IsRadixApplicationValid(ra)
if err != nil {
return fmt.Errorf("RadixConfig is invalid:\n%w", err)
}

fmt.Fprintln(os.Stderr, "RadixConfig is valid")
return nil
},
}

func printRA(ra *radixv1.RadixApplication) error {
b, err := yaml.Marshal(ra)
if err != nil {
return err
}

fmt.Fprintf(os.Stdout, "%s", b)
return nil
}

func init() {
validateCmd.AddCommand(validateRadixConfigCmd)
validateRadixConfigCmd.Flags().StringP("config-file", "f", "radixconfig.yaml", "Name of the radixconfig file. Defaults to radixconfig.yaml in current directory")
validateRadixConfigCmd.Flags().BoolP("print", "p", false, "Print parsed config file")
setVerbosePersistentFlag(validateRadixConfigCmd)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.0
require (
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.0
github.com/equinor/radix-common v1.5.0
github.com/equinor/radix-operator v1.43.0
github.com/equinor/radix-operator v1.44.1
github.com/fatih/color v1.15.0
github.com/go-openapi/errors v0.20.4
github.com/go-openapi/runtime v0.26.0
Expand All @@ -18,6 +18,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
k8s.io/utils v0.0.0-20230505201702-9f6742963106
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -90,5 +91,4 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/secrets-store-csi-driver v1.3.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinor/radix-common v1.5.0 h1:z5hQHlKG2x16/NnV4b9ynf9n5ZageYUewE4MANdA96Y=
github.com/equinor/radix-common v1.5.0/go.mod h1:UZ69U56VFtTxABi5JjGdaqn9Df5ilfTTqzUQ0riofVM=
github.com/equinor/radix-operator v1.43.0 h1:25vXiqy0BZrTu1VjA/kFgP7IGQ/UoFAtx0QCq0Irm8E=
github.com/equinor/radix-operator v1.43.0/go.mod h1:fa1yXgnhluKtqlBP+k/VldbkpFRpkK5eBGfGhuilqNc=
github.com/equinor/radix-operator v1.44.1 h1:ogaVIERfEbeD5QexsXXZSN6k4v4hgbtFDFHoLgBkNjA=
github.com/equinor/radix-operator v1.44.1/go.mod h1:9YBEz5p/s2YCL8FGzjvRUXgPjtrBojnjw/tAXQNzwx0=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
Expand Down
Loading