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

Add support for outputting kube config credentials from AKS #953

Merged
merged 14 commits into from
Apr 26, 2018

Conversation

jjcollinge
Copy link
Contributor

This PR adds support for outputting the fields (from the kube config) necessary to input into the Kubernetes Terraform provider. This will enable a Terraform plan to stand up an AKS cluster and add Kubernetes objects directly - without having to use kubectl.

This will be used to deploy the agents needed for the container solution enabled by #952

cc: @sozercan

@paultyng
Copy link
Contributor

paultyng commented Mar 8, 2018

I think this also addresses #934?

return nil
}

//TODO: Hide these
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, agree with this comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh - I'd suggest these would also be good candidates to move out to ./azurerm/helpers/kubernetes?

@@ -454,3 +539,73 @@ func resourceAzureRMKubernetesClusterServicePrincipalProfileHash(v interface{})

return hashcode.String(buf.String())
}

func base64Decode(str string) (string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a huge thing, but would be nice to throw some unit testing on this and/or getKubeConfig

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this function, fwiw - we could just check that err != nil below?

@lawrencegripper
Copy link
Contributor

Yeah it should address #934. I'm using it like so to pass credentials to the kubernetes provider

resource "azurerm_kubernetes_cluster" "aks" {
    name        = "akc-${random_integer.random_int.result}"
    location    = "${azurerm_resource_group.test.location}"
    dns_prefix  = "akc-${random_integer.random_int.result}"

    resource_group_name = "${azurerm_resource_group.test.name}"
    kubernetes_version  = "1.8.7"


    linux_profile {
        admin_username = "${var.linux_admin_username}"
        ssh_key {
            key_data = "${var.linux_admin_ssh_publickey}"
        }
    }

    agent_pool_profile {
        name        = "agentpool"
        count       = "2"
        vm_size     = "Standard_DS2_v2"
        os_type     = "Linux"
    }

    service_principal {
        client_id     = "${var.client_id}"
        client_secret = "${var.client_secret}"
    }
}

provider "kubernetes" {
  host     = "${azurerm_kubernetes_cluster.aks.kube_config.0.host}"

  client_certificate     = "${base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)}"
  client_key             = "${base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)}"
  cluster_ca_certificate = "${base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)}"
}

resource "kubernetes_namespace" "monitoring" {
  metadata {
    name = "monitoring"
  }
}

@paultyng
Copy link
Contributor

paultyng commented Mar 8, 2018

This also needs some acceptance test coverage, testing the outputs at least in the existing tests.

@jjcollinge
Copy link
Contributor Author

@paultyng thanks for the review 👍 - I'll update ASAP.

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @jjcollinge

Thanks for this PR - this is looking really good! I've left some comments in-line but they basically follow what @paultyng has suggested, such that if we can add some tests for this (and potentially move the Kubernetes stuff into it's own package) this should be good to merge :)

Thanks!

}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor/not a blocker: we can actually in-line these statements to condense it like so:

if profile != nil {
  if accessProfile := profile.AccessProfile; accessProfile != nil {
    if kubeConfig := accessProfile.KubeConfig; kubeConfig != nil {
      return flattenedKubeConfig(kubeConfig)
    }
  }
}

return string(data), false
}

func getKubeConfig(config *string) *KubeConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor it might be worth pulling these out into a kubernetes specific package inside ./azurerm/helpers/kubernetes?

@@ -454,3 +539,73 @@ func resourceAzureRMKubernetesClusterServicePrincipalProfileHash(v interface{})

return hashcode.String(buf.String())
}

func base64Decode(str string) (string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this function, fwiw - we could just check that err != nil below?


configStr, error := base64Decode(*config)
if error == false && configStr != "" {
log.Println(config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given this is printing sensitive information, we should probably remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, didn't mean to leave that line in!

return nil
}

//TODO: Hide these
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh - I'd suggest these would also be good candidates to move out to ./azurerm/helpers/kubernetes?


* `kube_config.0.username` - The username used to authenticate to the Kubernetes cluster

* `kube_config.0.password` - The password or token used to authenticate to the Kubernetes cluster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we update this to match the other documentation? In general we'd show these like so:

* `kube_config` - a `kube_config` block as defined below:

---

* `client_key` - The base64-encoded Client Key used to authenticate to the Kubernetes Cluster

# ...

it may be worth adding an output to the example above to demonstrate how to access them?

@jjcollinge
Copy link
Contributor Author

Thanks for the review @tombuildsstuff - I'll get this fixed up tomorrow 😀

@jjcollinge
Copy link
Contributor Author

@paultyng @tombuildsstuff - updated as per reviews

@paultyng
Copy link
Contributor

paultyng commented Mar 9, 2018

FYI the testdata folder is the idiomatic place to store test data. https://golang.org/cmd/go/#hdr-Test_packages

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @jjcollinge

Thanks for pushing the latest changes, I've left one comment (which I'll push a commit for - I hope you don't mind!) - but this otherwise LGTM 👍

Thanks!


output "host" {
value = "${azurerm_kubernetes_cluster.test.kube_config.0.host}"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to push a commit to update the test to ensure these values are actually set

@jjcollinge
Copy link
Contributor Author

@tombuildsstuff thanks for the review - just a word of warning in case you're running the acceptance tests, AKS is currently having a few 'reliability' issues in certain regions. I've found I can only deploy into centralus at the moment.

@tombuildsstuff
Copy link
Contributor

@jjcollinge thanks for the heads up - I can confirm that issue, I'll give it a few hours for this to recover and try running the tests for this later today; but this otherwise LGTM :)

@tombuildsstuff tombuildsstuff added this to the 1.3.1 milestone Mar 15, 2018
@tombuildsstuff
Copy link
Contributor

@jjcollinge heads up the API still appeared to be broken yesterday - so I've reached out to MS through some internal channels.. I've bumped this to the next release for the moment, sorry for the delay getting this merged!

@tombuildsstuff tombuildsstuff added the upstream/microsoft Indicates that there's an upstream issue blocking this issue/PR label Mar 15, 2018
@jjcollinge
Copy link
Contributor Author

jjcollinge commented Mar 16, 2018

@tombuildsstuff stuff - no worries! I was thinking of adding another output field called 'source' which contains the original encoded config. This will be useful for people who want to inject it straight into kubectl rather than using the Kubernetes Terraform provider - thoughts?

@tombuildsstuff
Copy link
Contributor

@jjcollinge sounds good to me - although I'd suggest the fields named kubeconfig or raw_kubeconfig or something; so it's clearer what it is :)

@tombuildsstuff tombuildsstuff modified the milestones: 1.3.1, 1.3.2 Mar 28, 2018
@tombuildsstuff tombuildsstuff modified the milestones: 1.3.2, 1.3.3 Apr 4, 2018
@lawrencegripper
Copy link
Contributor

Hopefully this should be good to test now

@tombuildsstuff tombuildsstuff removed the upstream/microsoft Indicates that there's an upstream issue blocking this issue/PR label Apr 10, 2018
@tombuildsstuff
Copy link
Contributor

hey @jjcollinge

Thanks for pushing the latest updates - upon re-running the tests I noticed a compilation error caused by upgrading to v15 of the Azure SDK - which I've pushed a commit to fix (I hope you don't mind!) - which means that these tests should now run again.

Thanks!

@tombuildsstuff tombuildsstuff added this to the Soon milestone Apr 25, 2018
@tombuildsstuff
Copy link
Contributor

@jjcollinge I've spent some time looking into this this morning and there was a breaking change as part of the latest Azure SDK; as such I've pushed a commit to fix that which enables this to work as expected, I hope you don't mind.

@tombuildsstuff tombuildsstuff modified the milestones: Soon, 1.4.0 Apr 25, 2018
@tombuildsstuff
Copy link
Contributor

👋 hey @jjcollinge

Just to give an update here: so I've confirmed the tests for this look good - the failures here are AKS capacity issues:

screen shot 2018-04-25 at 20 02 33

I've kicked off one final test of TestAccAzureRMKubernetesCluster_basic - providing that passes (which it did for me earlier) - we'll merge this PR and release it as a part of v1.4.0 in the morning :)

Thanks!

@tombuildsstuff
Copy link
Contributor

The _basic test passes:

screen shot 2018-04-26 at 11 07 21

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@tombuildsstuff tombuildsstuff merged commit 587c815 into hashicorp:master Apr 26, 2018
tombuildsstuff added a commit that referenced this pull request Apr 26, 2018
@tombuildsstuff
Copy link
Contributor

hey @jjcollinge

Just to let you know that v1.4.0 of the Azure Provider has been released which includes support for this :)

Thanks!

@jjcollinge
Copy link
Contributor Author

Great! Thanks for sticking with this @tombuildsstuff - appreciate it's not been a smooth process!

@jjcollinge jjcollinge deleted the lg-akscreds branch April 26, 2018 17:24
@lawrencegripper
Copy link
Contributor

Example works on the new release :)

https://github.com/lawrencegripper/azure-aks-terraform

@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants