Skip to content

Commit

Permalink
printing to stderr and adding exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
SethHollandsworth committed Jul 19, 2024
1 parent 8f34d29 commit 8715cdb
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions cmd/podspec-to-arm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,47 +72,48 @@ func main() {
Long: desc,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage podspec-to-arm <input-file-name> [--output-file-name <output file>] [--print-json]")
return
fmt.Fprintln(os.Stderr, "Usage podspec-to-arm <input-file-name> [--output-file-name <output file>] [--print-json]")
os.Exit(1)
}

fileName := args[0]

// create pod object from podspec yaml file
file, err := os.ReadFile(fileName)
if err != nil {
fmt.Println("Error reading file: ", err)
return
fmt.Fprintln(os.Stderr, "Error reading file: ", err)
os.Exit(1)
}

pod := v1.Pod{}
err = yaml.Unmarshal(file, &pod)
if err != nil {
fmt.Println("Error unmarshalling YAML: ", err)
return
fmt.Fprintln(os.Stderr, "Error unmarshalling YAML: ", err)
os.Exit(1)
}

aciMocks := createNewACIMock()
provider, err := createTestProvider(aciMocks, NewMockConfigMapLister(),
NewMockSecretLister(), NewMockPodLister(), nil)
if err != nil {
fmt.Println("got error init provider")
fmt.Println(err)
fmt.Fprintln(os.Stderr, "got error init provider")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

// read secrets from file
secretsMap := map[string]corev1.Secret{}
if k8secrets != "" {
secretsfile, err := os.ReadFile(k8secrets)
if err != nil {
fmt.Println("Error reading secrets file:", err)
return
fmt.Fprintln(os.Stderr, "Error reading secrets file:", err)
os.Exit(1)
}
err = yaml.Unmarshal([]byte(secretsfile), &secretsMap)
if err != nil {
fmt.Println("Error unmarshalling secrets:", err)
fmt.Printf("Make sure the file is of the format: \n<secret-name>:\n apiVersion: v1\n kind: Secret\n metadata\n name: <secret-name>\n data:\n <key>: <value>\n")
return
fmt.Fprintln(os.Stderr, "Error unmarshalling secrets:", err)
fmt.Fprintf(os.Stderr, "Make sure the file is of the format: \n<secret-name>:\n apiVersion: v1\n kind: Secret\n metadata\n name: <secret-name>\n data:\n <key>: <value>\n")
os.Exit(1)
}

// fill in values in pod with secrets data
Expand All @@ -125,13 +126,13 @@ func main() {
key := envVar.ValueFrom.SecretKeyRef.Key
secret, ok := secretsMap[secretName]
if !ok {
fmt.Printf("Secret %s not found in secrets file\n", secretName)
return
fmt.Fprintf(os.Stderr, "Secret %s not found in secrets file\n", secretName)
os.Exit(1)
}
val, ok := secret.Data[key]
if !ok {
fmt.Printf("Key %s not found in secret %s\n", key, secretName)
return
fmt.Fprintf(os.Stderr, "Key %s not found in secret %s\n", key, secretName)
os.Exit(1)
}

// remove trailing newline characters
Expand All @@ -146,15 +147,15 @@ func main() {
if k8configmaps != "" {
configmapfile, err := os.ReadFile(k8configmaps)
if err != nil {
fmt.Println("Error reading configmaps file:", err)
return
fmt.Fprintln(os.Stderr, "Error reading configmaps file:", err)
os.Exit(1)
}
err = yaml.Unmarshal([]byte(configmapfile), &configsMap)

if err != nil {
fmt.Println("Error unmarshalling configmaps:", err)
fmt.Printf("Make sure the file is of the format: \n<configmap-name>:\n apiVersion: v1\n kind: ConfigMap\n metadata\n name: <configmap-name>\n data:\n <key>: <value>\n")
return
fmt.Fprintln(os.Stderr, "Error unmarshalling configmaps:", err)
fmt.Fprintf(os.Stderr, "Make sure the file is of the format: \n<configmap-name>:\n apiVersion: v1\n kind: ConfigMap\n metadata\n name: <configmap-name>\n data:\n <key>: <value>\n")
os.Exit(1)
}

// fill in values in pod with configmaps data
Expand All @@ -167,13 +168,13 @@ func main() {
key := envVar.ValueFrom.ConfigMapKeyRef.Key
config, ok := configsMap[configName]
if !ok {
fmt.Printf("ConfigMap %s not found in configmaps file\n", configName)
return
fmt.Fprintf(os.Stderr, "ConfigMap %s not found in configmaps file\n", configName)
os.Exit(1)
}
val, ok := config.Data[key]
if !ok {
fmt.Printf("Key %s not found in configmap %s\n", key, configName)
return
fmt.Fprintf(os.Stderr, "Key %s not found in configmap %s\n", key, configName)
os.Exit(1)
}
envVar.Value = string(val)
}
Expand All @@ -186,7 +187,8 @@ func main() {
// create container group
cg, err := provider.CreatePodData(context.Background(), &pod, secretsMap, configsMap)
if err != nil {
fmt.Println("Error creating pod data", err)
fmt.Fprintln(os.Stderr, "Error creating pod data", err)
os.Exit(1)
}
// fill in namespace if not present
if pod.Namespace == "" {
Expand Down Expand Up @@ -242,7 +244,8 @@ func main() {

arm_json_bytes, err := json.MarshalIndent(armTemplate, "", "\t")
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

outputjson := string(arm_json_bytes)
Expand All @@ -257,14 +260,14 @@ func main() {
// write output to file
f, err := os.Create(outFileName)
if err != nil {
fmt.Println("Error creating output file:", err)
return
fmt.Fprintln(os.Stderr, "Error creating output file:", err)
os.Exit(1)
}
defer f.Close()
n, err := f.Write([]byte(outputjson))
if err != nil {
fmt.Println("Error writing to output file:", err)
return
fmt.Fprintln(os.Stderr, "Error writing to output file:", err)
os.Exit(1)
}
fmt.Printf("Written %d bytes to file %s\n", n, outFileName)
},
Expand Down Expand Up @@ -304,8 +307,8 @@ func createTestProvider(aciMocks *MockACIProvider, configMapMocker *MockConfigMa

err := setAuthConfig()
if err != nil {
fmt.Println(err)
//return nil, err
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if kubeClient == nil {
Expand Down Expand Up @@ -404,7 +407,8 @@ func injectEnvVars(containergroup *azaciv2.ContainerGroup) {
k8EnvVars := []*azaciv2.EnvironmentVariable{}
err := json.Unmarshal([]byte(k8EnvVarsString), &k8EnvVars)
if err != nil {
fmt.Println("Error unmarshalling environment variables:", err)
fmt.Fprintln(os.Stderr, "Error unmarshalling environment variables:", err)
os.Exit(1)
}
for i := range containergroup.Properties.Containers {
container := containergroup.Properties.Containers[i]
Expand Down Expand Up @@ -440,8 +444,3 @@ func injectVolumeMount(containergroup *azaciv2.ContainerGroup, volumename string
}
containergroup.Properties.Volumes = append(containergroup.Properties.Volumes, k8Volume)
}



//TODO:
//2. Better error messages -> check various failures and throw better messages

0 comments on commit 8715cdb

Please sign in to comment.