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

Added annotation validation methods for Custom Resources #220

Closed
wants to merge 4 commits into from
Closed

Added annotation validation methods for Custom Resources #220

wants to merge 4 commits into from

Conversation

nmacherey
Copy link

@nmacherey nmacherey commented Dec 30, 2018

What this PR does / why we need it:

Add annotation support for CDR, in ListPlugins, ListConsumers, ListCredentials and in CRD event handling.

Which issue this PR fixes

Special notes for your reviewer:
My knowledge of GO is very poor, I've added tests and deployed it on my local kubernetes cluster and things seams to work fine. Anyway the code may need some customization.

@codecov-io
Copy link

codecov-io commented Dec 30, 2018

Codecov Report

Merging #220 into master will increase coverage by 0.24%.
The diff coverage is 24.45%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #220      +/-   ##
=========================================
+ Coverage   21.85%   22.1%   +0.24%     
=========================================
  Files          24      24              
  Lines        2375    2552     +177     
=========================================
+ Hits          519     564      +45     
- Misses       1812    1938     +126     
- Partials       44      50       +6
Impacted Files Coverage Δ
internal/ingress/controller/kong.go 5.17% <ø> (ø) ⬆️
internal/ingress/controller/controller.go 0% <ø> (ø) ⬆️
internal/ingress/controller/store/store.go 0% <0%> (ø) ⬆️
internal/ingress/annotations/class/main.go 71.42% <68.18%> (ø) ⬆️
internal/ingress/annotations/parser/main.go 81.92% <83.33%> (+1.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 12d1e52...c2f1bc1. Read the comment docs.

// 4 - ingress with specific class | different annotation on ingress
if ingress == "" && IngressClass == DefaultClass {
return true
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract the functionality out into a function rather than repeating it multiple times?

Copy link
Member

@hbagdi hbagdi left a comment

Choose a reason for hiding this comment

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

I just pushed a commit to master to fix the CI issue. Please rebase your branch on top of it.
I think we can remove some unneeded tests and remove some duplicate code.
This is getting into good shape!

@@ -15,6 +15,30 @@ Following CRDs enables users to declaratively configure all aspects of Kong:
- [**KongCredential**](#kongcredential): These resources map to
credentials (key-auth, basic-auth, etc) that belong to consumers.

**IMPORTANT NOTE**: Kong Custom Resources are using the `kubernetes.io/ingress.class` annotation which defaults to `kong`. In
case you have more than one Kong Ingress deployed on your cluster, make sure you label all your ressources with the appropriate
annotation. For example:
Copy link
Member

Choose a reason for hiding this comment

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

  1. The ingress class annotation defaults to nginx and not kong, so let's update that.
  2. Please respect the 80 column length as is the case in the rest of this file.
  3. Let's move this note to the bottom of the file than here.
  4. KongIngress custom resource doesn't support the ingress.class annotation as per this PR, so let's mention the resources that support the annotation explicitly.
  5. In case you have more than one Kong Ingress deployed

  6. Typo > ressources

}
}

func TestIsValidPlugin(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Are these tests necessary to be repeated for each type since the method now takes in ObjectMeta?
If not, let remove those as additional tests are redundant.

@@ -34,6 +37,32 @@ func buildIngress() *extensions.Ingress {
}
}

func buildPlugin() *pluginv1.KongPlugin {
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here, do these tests need to be repeated? The don't seem to be testing anything specific to a plugin, consumer or ingress but are operating on the ObjectMeta struct.

if !class.IsValid(&plugin.ObjectMeta) {
glog.Infof("ignoring delete for plugin %v based on annotation %v", plugin.Name, class.IngressKey)
return
}
Copy link
Member

Choose a reason for hiding this comment

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

There seems to be a lot of duplication of code here.
Can we create a method for checking the validity of the resource and then reuse that across all 3 cache handlers?

Copy link
Author

Choose a reason for hiding this comment

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

I've created 3 new methods in class package:

  • CanAddResource
  • CanDeleteResource
  • CanUpdateResource

I've also renamed the event handler annotatedCrdEventHandler which is now common to all CRD that supports annotations.

Copy link
Member

Choose a reason for hiding this comment

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

The new code seems very odd in the way it is organized.

Organizing the code as follows:

		AddFunc: func(obj interface{}) {
                   var p *pluginv1.KongPlugin
                   if p, ok := obj.(*pluginv1.KongPlugin); !ok {
                     // err  
                   }
		   objectMeta = &p.ObjectMeta			
                   if ok := class.IsValid(objectMeta); !ok
                     return
		    }
		    updateCh.In() <- Event{
		       Type: ConfigurationEvent,
		       Obj:  obj,
		     }
		},

This helps in a few ways:

  • Avoid duplicate logic per type
  • IsValid is the only method that is called, making it easier to test and introduces changes in future.

Copy link
Author

Choose a reason for hiding this comment

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

@hbagdi do you prefer 1 event handler per resource because the code you've suggested means this... I tried to keep only one handler anyway I can fall back to 3 handlers and have your last suggestion.

Copy link
Member

Choose a reason for hiding this comment

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

I think 3 handlers are okay, since we need to type cast. But we should keep the hanlders small and have minimal logic as possible.

Copy link
Author

Choose a reason for hiding this comment

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

Ok pushed.

@hbagdi
Copy link
Member

hbagdi commented Jan 8, 2019

@nmacherey Manually merged in with some bug fixes and corrections.
Thank you for contributing this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants