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 DaemonSet and ClusterRole resources #229

Merged

Conversation

chris-codaio
Copy link

@chris-codaio chris-codaio commented Nov 27, 2018

Brings in a couple of resources from #100

@terraform-providers/ecosystem

@ghost ghost added the size/XXL label Nov 27, 2018
Copy link
Member

@alexsomesan alexsomesan left a comment

Choose a reason for hiding this comment

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

Hi! This is really cool stuff!
Thank you for taking the time to put these two resources together.

I've looked over and there are a few comments here and there, mostly about aligning the codebase to a few standard practices we're trying to stick to.

Please have a look over my comments and in the mean time I will review the DaemonSet part of this PR.

Thanks again for all the hard work!

}
log.Printf("[INFO] Reading cluster role %s", name)
cRole, err := conn.RbacV1().ClusterRoles().Get(name, metav1.GetOptions{})
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Here, we need more elaborate handling of err.
In particular, in case the error is of a NotFound kind, the ID of the resource should be set to d.SetId("") and no error should be returned. This instructs Terraform to remove the resource from the state.
You can check the type of error returned in err by using these client-go helper functions: https://godoc.org/k8s.io/apimachinery/pkg/api/errors

Copy link
Author

Choose a reason for hiding this comment

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

Done

}

log.Printf("[INFO] Updating cluster role %q: %v", name, cRole)
out, err := conn.RbacV1().ClusterRoles().Update(&cRole)
Copy link
Member

Choose a reason for hiding this comment

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

The .Update() method of client-go resources is actually not doing an in-place update, but rather a replace type of operation, meaning it replaces the entire API object with the new one passed in.
This does not map well to Terraform's expectations of an Update action, which should be an in-place modification of the existing resource.

Whenever this is not technically possible or not desired, the proper way to implement this in Terraform is to let it do a Delete/Create sequence. This is done by tagging attributes that cannot be updated with ForceNew: true.

If you do indeed want to implement the Update function, the changes to the resource should be done via .Patch() method of client-go. This takes in a list of JSONPatch style operations and this list is what the Terraform Update function should construct before calling .Patch()

Copy link
Author

Choose a reason for hiding this comment

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

Done


log.Printf("[INFO] cluster role %s deleted", name)

d.SetId("")
Copy link
Member

Choose a reason for hiding this comment

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

Setting ID to "" is not actually needed in Delete functions. Terraform will do that automatically.

Copy link
Author

Choose a reason for hiding this comment

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

Nice - done.

log.Printf("[INFO] Checking cluster role %s", name)
_, err = conn.RbacV1().ClusterRoles().Get(name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please make use of the client-go error helpers I mentioned earlier, instead of the direct comparisons in this line?

Copy link
Author

Choose a reason for hiding this comment

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

Done

resources = ["pods", "pods/log"]
verbs = ["get", "list"]
}
}`, name)
Copy link
Member

Choose a reason for hiding this comment

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

Please make sure all these inline HCL blocks are terraform fmt formatted and the last closing bracket of the resource is always followed by a new line character.

This is needed to we can eventually make use of a new linter / formatter in CI: https://github.com/katbyte/terrafmt

Copy link
Author

Choose a reason for hiding this comment

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

Done

Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Copy link
Member

Choose a reason for hiding this comment

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

Can you please reuse the labelSelectorFields function for populating this Schema section?
You can find it in kubernetes/schema_label_selector.go

Copy link
Author

Choose a reason for hiding this comment

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

Done

Description: "An object that describes the pod that will be created. The DaemonSet will create exactly one copy of this pod on every node that matches the template's node selector (or on every node if no node selector is specified). More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Copy link
Member

Choose a reason for hiding this comment

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

Same here, please reuse the podTemplateFields() function to populate this schema section.

The function is in kubernetes/schema_pod_template.go

Copy link
Author

Choose a reason for hiding this comment

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

Done

@chris-codaio
Copy link
Author

Thanks for the review! All current comments addressed with a new commit. PTAL when you can.

@chris-codaio
Copy link
Author

@alexsomesan mind taking another look at this when you have a chance? I've addressed all of your comments.

@alexsomesan
Copy link
Member

@chrisleck I'll try to review as soon as I get a chance.

@chris-codaio
Copy link
Author

@alexsomesan - pinging to make sure this is still on your radar to review. Don't want to be pushy, but I do have a project blocked on this :)

@RobMaskell
Copy link

Just to add my $0.02, I'm not exactly blocked by this but I think I have a bug to report but as I'm currently provisioning the ClusterRole in an unconventional way I wanted to test with this first.

@alexsomesan
Copy link
Member

@chrisleck I haven't forgotten you! I'll get to this PR shortly. I'm at KubeCon this week so operating at low capacity here.

@chris-codaio
Copy link
Author

@alexsomesan - thanks for the update and enjoy the conference!

@bukzor
Copy link

bukzor commented Dec 22, 2018

I don't have enough context to know whether this matters for this PR, but I went and used the functionality added here and found two missing bits of API. volume.host_path.type is unsupported, as well as volume_mount.mount_propagation.

  1. https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
  2. https://kubernetes.io/docs/concepts/storage/volumes/#mount-propagation

@chris-codaio
Copy link
Author

@alexsomesan
Copy link
Member

alexsomesan commented Jan 10, 2019 via email

@alexsomesan
Copy link
Member

alexsomesan commented Jan 10, 2019

@chrisleck, are there any acceptance tests for the DaemonSet resource? Nevermind, I found them. The diff view was folding them away.

@alexsomesan
Copy link
Member

@chrisleck I'm running into a few small issues trying to test this.
First, I need to make this change to get it to build:

diff --git a/kubernetes/structures_daemonset.go b/kubernetes/structures_daemonset.go
index c553bf0..dfcae87 100644
--- a/kubernetes/structures_daemonset.go
+++ b/kubernetes/structures_daemonset.go
@@ -77,7 +77,7 @@ func expandDaemonSetSpec(daemonset []interface{}) (appsv1.DaemonSetSpec, error)
        if err != nil {
                return obj, err
        }
-       obj.Template = template
+       obj.Template = *template

        return obj, nil
 }

After that, I'm seeing this test failure:

~/workspace/terraform-provider-kubernetes(add-daemonset-and-clusterrole*) » TESTARGS="-run '^TestAccKubernetesClusterRole_basic'" make testacc                                        alex@alexs-macbook
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run '^TestAccKubernetesClusterRole_basic' -timeout 120m
?   	github.com/terraform-providers/terraform-provider-kubernetes	[no test files]
=== RUN   TestAccKubernetesClusterRole_basic
--- FAIL: TestAccKubernetesClusterRole_basic (0.46s)
    testing.go:538: Step 1 error: Error applying: 1 error occurred:
        	* kubernetes_cluster_role.test: 1 error occurred:
        	* kubernetes_cluster_role.test: Failed to update ClusterRole: invalid resource name "/tf-acc-test-alex-dv1z7k82d2": [may not contain '/']

FAIL
FAIL	github.com/terraform-providers/terraform-provider-kubernetes/kubernetes	1.182s
make: *** [testacc] Error 1
------------------------------------------------------------

@chris-codaio
Copy link
Author

Hmm - not seeing that. I'll try re-syncing to head and make sure we're clean.

@ghost ghost added the documentation label Jan 12, 2019
@chris-codaio
Copy link
Author

Ah - turns out I had pushed an older branch :(

@chris-codaio
Copy link
Author

Updated with a fresh sync to master and validated tests are running. PTAL :)

@deedubs
Copy link

deedubs commented Jan 16, 2019

This is great @chrisleck. With these landing, I believe we'll reach a tipping point where we can start to consider terraform a strong contender for managing core cluster state on k8s.

},
"template": {
Type: schema.TypeList,
Description: "An object that describes the pod that will be created. The DaemonSet will create exactly one copy of this pod on every node that matches the template's node selector (or on every node if no node selector is specified). More info: https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template",
Copy link
Contributor

Choose a reason for hiding this comment

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

This description should probably link to the DaemonSet specific
https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#pod-template documentation or to the more general pod documentation https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/#pod-templates

Copy link
Author

Choose a reason for hiding this comment

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

Done

@chrisdewar
Copy link

Would be good to get this merged if all issues have been addressed.

@orkaa
Copy link

orkaa commented Jan 22, 2019

Hi! Really looking forward to getting this merged since I'm just setting up a new cluster the terraform way. ❤️

How soon can I expect this to be released? I'm deciding if I want to wait or develop a workaround in the meantime.

Thanks!

Copy link
Member

@alexsomesan alexsomesan left a comment

Choose a reason for hiding this comment

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

Acceptance tests are all green in CI.
This looks good overall.
I will merge and follow up with a few small touch-ups (removing redundant assignments like out := &appsv1.DaemonSet{})

Thanks for your good work!

@alexsomesan alexsomesan merged commit f9354bd into hashicorp:master Jan 22, 2019
@jodersky
Copy link

jodersky commented Feb 5, 2019

Awesome that this got merged! Is there already a release planned with these new features?

@ranimufid
Copy link

@alexsomesan, may we please know roughly when the upcoming release with this merged content is going to happen?

@alexsomesan
Copy link
Member

I was hoping to get more of the pending changes in before a release.

But I can also do a release on Monday since this is something a lot of people are waiting for.

@ranimufid
Copy link

But I can also do a release on Monday since this is something a lot of people are waiting for.

That would be greatly appreciated @alexsomesan!

@alexsomesan
Copy link
Member

Ok, will do :)

@Natalique
Copy link

Ok, will do :)

Thanks, can't wait! @alexsomesan

@yacut
Copy link

yacut commented Feb 18, 2019

@alexsomesan I've already tested it. It works great. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 21, 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.