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

Document how to add 3rd party resources, such as OpenShift resources #477

Closed
benjaminapetersen opened this issue Sep 12, 2018 · 15 comments
Closed
Assignees

Comments

@benjaminapetersen
Copy link
Contributor

benjaminapetersen commented Sep 12, 2018

When attempting to sdk.Watch an OpenShift Route via:

sdk.Watch("route.openshift.io/v1", "Route", namespace, resyncPeriod)

I get errors like:

ERROR: logging before flag.Parse: E0911 17:24:14.258632   57519 runtime.go:66] Observed a panic: &errors.errorString{s:"failed to decode json data with gvk(route.openshift.io/v1, Kind=Route): no kind \"Route\" is registered for version \"route.openshift.io/v1\""} (failed to decode json data with gvk(route.openshift.io/v1, Kind=Route): no kind "Route" is registered for version "route.openshift.io/v1")
$HOME/go/src/github.com/openshift/console-operator/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:72
$HOME/go/src/github.com/openshift/console-operator/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:65
$HOME/go/src/github.com/openshift/console-operator/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:51
/usr/local/Cellar/go/1.11/libexec/src/runtime/asm_amd64.s:522
/usr/local/Cellar/go/1.11/libexec/src/runtime/panic.go:513
/// etc

However, this seems to have fixed the problem:

func init() {
    k8sutil.AddToSDKScheme(route.Install)
}

I'm suggesting one of two solutions:

  • Initially at least the User Guide should include a section for installing unknown resources
  • Alternatively, if sdk.Watch can be modified to handle resources generically, that may avoid the previous need.
@hasbro17
Copy link
Contributor

@benjaminapetersen adding new types to the SDK scheme via k8sutil.AddToSDKScheme() is the way to do it. That's how the custom resources are added right now.

I think just documenting it on the user-guide or somewhere else should be sufficient.
sdk.Watch can't do much to watch unknown resources. It comes back to all types being registered with the underlying scheme.

@ghost
Copy link

ghost commented Sep 18, 2018

@benjaminapetersen @hasbro17 same approach if I need to create a route from within the operator? Is there any simple example operator that creates and watches deployment, service and route which is typical for a webapp?

@shawn-hurley
Copy link
Member

shawn-hurley commented Sep 18, 2018

Whichever types you want to use in your operator need to be added to the scheme. for the core APIs (empty group) we add those by default.

metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})

If you want Deployments you will have to register apps/v1.

Example of how:

import (
    ....
    appsv1 "k8s.io/api/apps/v1"
)
...First couple lines of main.go...
k8sutil.AddToSDKScheme(appsv1.AddToScheme)`

@benjaminapetersen
Copy link
Contributor Author

@eivantsov my snippet above was for a route, that was my exact use case as well.

@benjaminapetersen
Copy link
Contributor Author

@eivantsov in total my main.go has this, its not a lot:

inport 	routev1 "github.com/openshift/api/route/v1"
// init will run before main
func init() {
	k8sutil.AddToSDKScheme(route.Install)
}

func watch(apiVersion, kind, namespace string, resyncPeriod int) {
	logrus.Infof("Watching %s, %s, %s, %d", apiVersion, kind, namespace, resyncPeriod)
	sdk.Watch(apiVersion, kind, namespace, resyncPeriod)
}

// and off we go
func main() {
        resyncPeriod := 60 * 60 * 10  // 10hrs.  no less. 
  	watch(routev1.GroupVersion.String(), "Route", namespace, resyncPeriod)
	watch(myResourceGroupVersion, "MyResource", namespace, resyncPeriod)
	sdk.Handle(stub.NewHandler())
	sdk.Run(context.TODO())    
}

@ghost
Copy link

ghost commented Sep 18, 2018

@benjaminapetersen thanks but for some reason, a simple

import (
	route "github.com/openshift/api/route"
	k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
)

func init() {
	k8sutil.AddToSDKScheme(appsv1.AddToScheme)
	k8sutil.AddToSDKScheme(route.Install)
}

Results in

cannot use route.Install (type func(*"github.com/openshift/api/vendor/k8s.io/apimachinery/pkg/runtime".Scheme) error) as type k8sutil.addToSchemeFunc in argument to k8sutil.AddToSDKScheme

@benjaminapetersen
Copy link
Contributor Author

You added the dependencies via dep, openshift/api ?
You should have /vendor/openshift/api directory if so.

@benjaminapetersen
Copy link
Contributor Author

(thats my guess)

@ghost
Copy link

ghost commented Sep 18, 2018

@benjaminapetersen yes, that was it, though I did go get it before. thanks

@benjaminapetersen
Copy link
Contributor Author

Nice!

@hasbro17 hasbro17 added the docs label Sep 18, 2018
@ghost
Copy link

ghost commented Sep 18, 2018

@benjaminapetersen thanks for your help. I think a short:

  1. Add to your imports
  2. Add to scheme
  3. Use in your function

with code snippets would be very helpful for those not familiar with Operators or k8s go lib in general.

I was able to write a simple operator that creates deployment, service account, rolebinding, service and a route exploring available examples and using some help from @benjaminapetersen

@shawn-hurley shawn-hurley self-assigned this Sep 19, 2018
@shawn-hurley
Copy link
Member

Closed by #509

@camilamacedo86
Copy link
Contributor

camilamacedo86 commented May 1, 2019

Hi folks,

It shows no longer work in this way. Could you please give me a hand in #1365?

c.c @eivantsov @hasbro17 @benjaminapetersen @shawn-hurley @jfchevrette

@ghost
Copy link

ghost commented May 2, 2019

@camilamacedo86
Copy link
Contributor

camilamacedo86 commented May 2, 2019

@eivantsov it worked the issue was just because I was adding the "routev1.AddToScheme(mgr.GetScheme())" after the "controller.AddToManager(mgr);" then the schema could not be added on it.

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

No branches or pull requests

4 participants