Skip to content

Commit

Permalink
cli work
Browse files Browse the repository at this point in the history
  • Loading branch information
frodopwns committed May 5, 2020
1 parent 400ebbb commit 1c6fd1a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 105 deletions.
145 changes: 68 additions & 77 deletions cli/cmd/createMi.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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 (
"bytes"
"encoding/json"
"fmt"
"html/template"
"os"
"strings"
"time"

"encoding/json"
"os/exec"

"github.com/google/shlex"
Expand Down Expand Up @@ -50,116 +39,118 @@ var createMiCmd = &cobra.Command{
Creates a new MI, assigns necessary roles, displays helpful follow-up commands.`,
RunE: func(cmd *cobra.Command, args []string) error {

// interpolate params into command
c := fmt.Sprintf(
"az identity create -g %s -n %s --subscription %s -o json",
config.ResourceGroup,
config.ManagedIdentity,
config.Subscription,
)

out, err := RunCommand(c)
if err != nil {
return err
commands := []struct {
Name string
C string
Target interface{}
}{
{
Name: "Create Managed Identity",
C: "az identity create -g {{ .ResourceGroup }} -n {{ .ManagedIdentity }} --subscription {{ .Subscription }} -o json",
Target: &config,
},
{
Name: "Assign Managed Identity Operator Role",
C: `az role assignment create --role "Managed Identity Operator" --assignee {{ .ServicePrincipal }} --scope "{{ .ManagedIdentityID }}"`,
Target: nil,
},
{
Name: "Assign Managed Identity Contributor Role",
C: `az role assignment create --role "Contributor" --assignee {{ .ManagedIdentityClientID }} --scope /subscriptions/{{ .Subscription }}`,
Target: nil,
},
}

err = json.Unmarshal(out, &config)
if err != nil {
return err
}
for _, c := range commands {
fmt.Println("Starting action:", c.Name)
fmt.Println("-------------------------")
t := template.Must(template.New(c.Name).Parse(c.C))

c2 := fmt.Sprintf(
`az role assignment create --role "Managed Identity Operator" --assignee %s --scope "%s"`,
config.ServicePrincipal,
config.ManagedIdentityID,
)

out, err = RunCommand(c2)
if err != nil {
return err
}
var rendered bytes.Buffer
err := t.Execute(&rendered, config)
if err != nil {
return err
}

for {
c3 := fmt.Sprintf(
`az role assignment create --role "Contributor" --assignee %s --scope /subscriptions/%s`,
config.ManagedIdentityClientID,
config.Subscription,
)
fmt.Println(rendered.String())
fmt.Println()
fmt.Println()

for {
out, err := RunCommand(rendered.String())
fmt.Println(string(out))
if err != nil {
if strings.Contains(string(out), "No matches in graph database") || strings.Contains(string(out), "does not exist in the directory") {
time.Sleep(10 * time.Second)
continue
}
return err
}

out, err = RunCommand(c3)
if err != nil {
if strings.Contains(string(out), "No matches in graph database for") || strings.Contains(string(out), "does not exist in the directory") {
time.Sleep(10 * time.Second)
continue
if c.Target != nil {
err = json.Unmarshal(out, c.Target)
if err != nil {
return err
}
}
return err

break
}

break
}

fmt.Println()
fmt.Println("Install AAD Pod Identity:")
fmt.Println("make install-aad-pod-identity")
fmt.Println()

tpl := `
apiVersion: "aadpodidentity.k8s.io/v1"
tpl := `apiVersion: "aadpodidentity.k8s.io/v1"
kind: AzureIdentity
metadata:
name: {{ .ManagedIdentity }}
namespace: azureoperator-system
name: {{ .ManagedIdentity }}
namespace: azureoperator-system
spec:
type: 0
ResourceID: /subscriptions/{{ .Subscription }}/resourcegroups/{{ .ResourceGroup }}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{{ .ManagedIdentity }}
ClientID: {{ .ManagedIdentityClientID }}
type: 0
ResourceID: /subscriptions/{{ .Subscription }}/resourcegroups/{{ .ResourceGroup }}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{{ .ManagedIdentity }}
ClientID: {{ .ManagedIdentityClientID }}
---
apiVersion: "aadpodidentity.k8s.io/v1"
kind: AzureIdentityBinding
metadata:
name: aso-identity-binding
namespace: azureoperator-system
name: aso-identity-binding
namespace: azureoperator-system
spec:
AzureIdentity: {{ .ManagedIdentity }}
Selector: aso_manager_binding`
fmt.Println()
AzureIdentity: {{ .ManagedIdentity }}
Selector: aso_manager_binding
`

fmt.Println("cat <<EOF | kubectl apply -f -")
t := template.Must(template.New("manifests").Parse(tpl))
err = t.Execute(os.Stdout, config)
err := t.Execute(os.Stdout, config)
if err != nil {
return err
}
fmt.Println("EOF")

return nil
},
}

func init() {
rootCmd.AddCommand(createMiCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
createMiCmd.PersistentFlags().StringVarP(&config.ResourceGroup, "resource-group", "g", "", "resource group to associate managed identity with")
createMiCmd.PersistentFlags().StringVarP(&config.ManagedIdentity, "managed-identity", "i", "", "managed identity name to create and set up")
createMiCmd.PersistentFlags().StringVarP(&config.ServicePrincipal, "service-principal", "p", "", "service principal associated with kube cluster cluster")
createMiCmd.PersistentFlags().StringVarP(&config.Subscription, "subscription", "s", "", "azure subscription to act against")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createMiCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func RunCommand(c string) ([]byte, error) {
fmt.Println(c)
parts, err := shlex.Split(c)
if err != nil {
return []byte(""), err
}

stmt := exec.Command(parts[0], parts[1:]...)
out, err := stmt.CombinedOutput()
fmt.Println(string(out))
return out, err
}
13 changes: 5 additions & 8 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package cmd

import (
Expand All @@ -12,16 +15,10 @@ import (

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cli",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "A collection of helpers for the ASO project",
Long: `This CLI tool contains helper commands for developing/testing the ASO project.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
22 changes: 2 additions & 20 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main

import "github.com/Azure/azure-service-operator/cli/cmd"
Expand Down

0 comments on commit 1c6fd1a

Please sign in to comment.