Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
audrastump committed Nov 14, 2024
1 parent 3b2f7d9 commit c3b33a7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
14 changes: 1 addition & 13 deletions cmd/generate-workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
type generateWorkflowCmd struct {
dest string
deployType string
fleet string
flagVariables []string
templateWriter templatewriter.TemplateWriter
}
Expand Down Expand Up @@ -48,7 +47,6 @@ with draft on AKS. This command assumes the 'setup-gh' command has been run prop

f.StringVarP(&gwCmd.dest, "destination", "d", currentDirDefaultFlagValue, "specify the path to the project directory")
f.StringVarP(&gwCmd.deployType, "deploy-type", "", "", "specify the k8s deployment type (helm, kustomize, manifests)")
f.StringVarP(&gwCmd.fleet, "fleet", "f", "", "specify if this is a fleet deployment (yes, no)")

f.StringArrayVarP(&gwCmd.flagVariables, "variable", "", []string{}, "pass template variables (e.g. --variable CLUSTERNAME=testCluster --variable DOCKERFILE=./Dockerfile)")
gwCmd.templateWriter = &writers.LocalFSWriter{}
Expand All @@ -63,17 +61,7 @@ func (gwc *generateWorkflowCmd) generateWorkflows() error {
var err error

flagVariablesMap = flagVariablesToMap(gwc.flagVariables)
if gwc.fleet == "" {
selection := &promptui.Select{
Label: "Is this a fleet deployment?",
Items: []string{"yes", "no"},
}
_, gwc.fleet, err = selection.Run()
if err != nil {
return err
}
}
flagVariablesMap["FLEET"] = gwc.fleet

if gwc.deployType == "" {
selection := &promptui.Select{
Label: "Select k8s Deployment Type",
Expand Down
17 changes: 0 additions & 17 deletions cmd/setup-gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ func fillSetUpConfig(sc *providers.SetUpCmd) error {
return fmt.Errorf("getting subscription ID: %w", err)
}

sc.Fleet = getFleet()
if err != nil {
return fmt.Errorf("getting fleet: %w", err)
}
} else {
sc.SubscriptionID = getSubscriptionID()
}
Expand Down Expand Up @@ -253,19 +249,6 @@ func getAzSubscriptionId(subLabels []providers.SubLabel, currentSub providers.Su
return subLabel.ID, nil
}

func getFleet() string {
selection := &promptui.Select{
Label: "Is this for a fleet deployment?",
Items: []string{"Yes", "No"},
}
_, selectResponse, err := selection.Run()
if err != nil {
return err.Error()
}

return selectResponse
}

func init() {
rootCmd.AddCommand(newSetUpCmd())
}
21 changes: 10 additions & 11 deletions pkg/providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,24 @@ func (sc *SetUpCmd) CreateServicePrincipal() error {
}

func (sc *SetUpCmd) assignSpRole(ctx context.Context) error {
if err := sc.assignRole(ctx, "b24988ac-6180-42a0-ab88-20f7382dd24c"); err != nil { // Contributor role ID
return err
}
if err := sc.assignRole(ctx, "5af6afb3-c06c-4fa4-8848-71a8aee05683"); err != nil { // Azure Kubernetes Fleet Manager RBAC Writer role ID
return err
}
log.Debug("Roles assigned successfully!")
return nil
}

func (sc *SetUpCmd) assignRole(ctx context.Context, roleId string) error {
roleAssignClient, err := createRoleAssignmentClient(sc.SubscriptionID)
if err != nil {
return fmt.Errorf("creating role assignment client: %w", err)
}

scope := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s", sc.SubscriptionID, sc.ResourceGroupName)
objectID := sc.spObjectId
var roleId string
if sc.Fleet == "Yes" {
log.Debug("Assigning Azure Kubernetes Fleet Manager RBAC Writer role to service principal...")
roleId = "5af6afb3-c06c-4fa4-8848-71a8aee05683" // Azure Kubernetes Fleet Manager RBAC Writer

} else {
log.Debug("Assigning contributor role to service principal...")
roleId = "b24988ac-6180-42a0-ab88-20f7382dd24c" // Contributor role ID

}
raUid := uuid.New().String()

fullAssignmentId := fmt.Sprintf("/%s/providers/Microsoft.Authorization/roleAssignments/%s", scope, raUid)
Expand All @@ -204,7 +204,6 @@ func (sc *SetUpCmd) assignSpRole(ctx context.Context) error {
return fmt.Errorf("creating role assignment: %w", err)
}

log.Debug("Role assigned successfully!")
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ env:
DOCKER_FILE: {{ .Config.GetVariableValue "DOCKERFILE" }}
BUILD_CONTEXT_PATH: {{ .Config.GetVariableValue "BUILDCONTEXTPATH" }}
NAMESPACE: {{ .Config.GetVariableValue "NAMESPACE" }}
FLEET: {{ .Config.GetVariableValue "FLEET" }}
FLEET_NAME: {{ .Config.GetVariableValue "FLEET_NAME" }}
{{`
jobs:
buildImage:
Expand Down Expand Up @@ -78,7 +80,7 @@ jobs:
runs-on: ubuntu-latest
needs: [buildImage]
steps:
# Checks out the repository this file is in
# Checks out the reposcditory this file is in
- uses: actions/checkout@v3

# Logs in with your Azure credentials
Expand All @@ -95,8 +97,17 @@ jobs:
with:
kubelogin-version: 'v0.0.25'

# gets credentials for a fleet
- name: Get fleet credentials
if: env.FLEET == 'true'
run: |
az fleet get-credentials -g ${{ env.CLUSTER_RESOURCE_GROUP }} -n ${{ env.FLEET_NAME }}
export KUBECONFIG=/home/runner/.kube/config
echo "KUBECONFIG=/home/runner/.kube/config" >> $GITHUB_ENV
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
if: env.FLEET != 'true'
uses: azure/aks-set-context@v3
with:
resource-group: ${{ env.CLUSTER_RESOURCE_GROUP }}
Expand All @@ -106,13 +117,15 @@ jobs:

# Checks if the AKS cluster is private
- name: Is private cluster
if: env.FLEET != 'true'
id: isPrivate
run: |
result=$(az aks show --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --query "apiServerAccessProfile.enablePrivateCluster")
echo "PRIVATE_CLUSTER=$result" >> "$GITHUB_OUTPUT"
# Deploys application based on given manifest file
- name: Deploys application
if: env.FLEET != 'true'
uses: Azure/k8s-deploy@v4
with:
action: deploy
Expand All @@ -123,4 +136,9 @@ jobs:
name: ${{ env.CLUSTER_NAME }}
private-cluster: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true' }}
namespace: ${{ env.NAMESPACE }}
- name: Deploys fleet application
if: env.FLEET == 'true'
run: |
kubectl config current-context
/usr/bin/kubectl apply --validate=false --request-timeout=60s -f ${{ env.DEPLOYMENT_MANIFEST_PATH }}/configmap.yaml,${{ env.DEPLOYMENT_MANIFEST_PATH }}/deployment.yaml,${{ env.DEPLOYMENT_MANIFEST_PATH }}/service.yaml --namespace ${{ env.NAMESPACE }}
`}}
14 changes: 14 additions & 0 deletions template/workflows/manifests/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ variables:
value: "default"
description: "the Kubernetes namespace"
versions: ">=0.0.1"
- name: "FLEET"
type: "boolean"
kind: "boolean"
default:
value: false
description: "specifies whether or not this is a fleet deployment"
versions: ">=0.0.1"
- name: "FLEET_NAME"
type: "string"
kind: "fleetName"
default:
value: ""
description: "name of the fleet to to deploy to, leave blank for no fleet"
versions: ">=0.0.1"

0 comments on commit c3b33a7

Please sign in to comment.