Skip to content

allow template instance controller to create CRDs #19396

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

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/cmd/openshift-controller-manager/controller/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
kexternalinformers "k8s.io/client-go/informers"
controllerapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
kclientsetinternal "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
Expand Down Expand Up @@ -47,6 +48,7 @@ type ControllerContext struct {
RouteInformers routeinformer.SharedInformerFactory
SecurityInformers securityinformer.SharedInformerFactory
GenericResourceInformer GenericResourceInformer
DynamicRestMapper *discovery.DeferredDiscoveryRESTMapper

// Stop is the stop channel
Stop <-chan struct{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func RunTemplateInstanceController(ctx ControllerContext) (bool, error) {
}

go templatecontroller.NewTemplateInstanceController(
ctx.DynamicRestMapper,
restConfig,
ctx.ClientBuilder.KubeInternalClientOrDie(saName),
ctx.ClientBuilder.OpenshiftInternalBuildClientOrDie(saName),
Expand Down
14 changes: 12 additions & 2 deletions pkg/cmd/openshift-controller-manager/controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/golang/glog"

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
cacheddiscovery "k8s.io/client-go/discovery/cached"
kclientsetexternal "k8s.io/client-go/kubernetes"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -117,6 +120,12 @@ func newControllerContext(
clientConfig.Burst = clientConfig.Burst/10 + 1
}

discoveryClient := cacheddiscovery.NewMemCacheClient(kubeExternal.Discovery())
dynamicRestMapper := discovery.NewDeferredDiscoveryRESTMapper(discoveryClient, meta.InterfacesForUnstructured)
dynamicRestMapper.Reset()

go wait.Until(dynamicRestMapper.Reset, 30*time.Second, stopCh)

openshiftControllerContext := origincontrollers.ControllerContext{
OpenshiftControllerConfig: config,

Expand All @@ -139,8 +148,9 @@ func newControllerContext(
RouteInformers: informers.GetRouteInformers(),
TemplateInformers: informers.GetTemplateInformers(),
GenericResourceInformer: informers.ToGenericInformer(),
Stop: stopCh,
InformersStarted: informersStarted,
Stop: stopCh,
InformersStarted: informersStarted,
DynamicRestMapper: dynamicRestMapper,
}

return openshiftControllerContext
Expand Down
58 changes: 35 additions & 23 deletions pkg/template/controller/templateinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kerrs "k8s.io/apimachinery/pkg/util/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/apis/authorization"
kapi "k8s.io/kubernetes/pkg/apis/core"
kclientsetinternal "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
Expand All @@ -40,7 +42,6 @@ import (
"github.com/openshift/origin/pkg/template/generated/informers/internalversion/template/internalversion"
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset"
templatelister "github.com/openshift/origin/pkg/template/generated/listers/template/internalversion"
restutil "github.com/openshift/origin/pkg/util/rest"
)

const readinessTimeout = time.Hour
Expand All @@ -51,9 +52,12 @@ const readinessTimeout = time.Hour
// using its own service account, first verifying that the requester also has
// permissions to instantiate.
type TemplateInstanceController struct {
restmapper meta.RESTMapper
config *rest.Config
templateClient templateclient.Interface
// TODO replace this with use of a codec built against the dynamic client
// (discuss w/ deads what this means)
dynamicRestMapper meta.RESTMapper
config *rest.Config
jsonConfig *rest.Config
templateClient templateclient.Interface

// FIXME: Remove then cient when the build configs are able to report the
// status of the last build.
Expand All @@ -72,18 +76,18 @@ type TemplateInstanceController struct {
}

// NewTemplateInstanceController returns a new TemplateInstanceController.
func NewTemplateInstanceController(config *rest.Config, kc kclientsetinternal.Interface, buildClient buildclient.Interface, templateClient templateclient.Interface, informer internalversion.TemplateInstanceInformer) *TemplateInstanceController {
func NewTemplateInstanceController(dynamicRestMapper *discovery.DeferredDiscoveryRESTMapper, config *rest.Config, kc kclientsetinternal.Interface, buildClient buildclient.Interface, templateClient templateclient.Interface, informer internalversion.TemplateInstanceInformer) *TemplateInstanceController {
c := &TemplateInstanceController{
restmapper: restutil.DefaultMultiRESTMapper(),
config: config,
kc: kc,
templateClient: templateClient,
buildClient: buildClient,
lister: informer.Lister(),
informer: informer.Informer(),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "openshift_template_instance_controller"),
readinessLimiter: workqueue.NewItemFastSlowRateLimiter(5*time.Second, 20*time.Second, 200),
clock: clock.RealClock{},
dynamicRestMapper: dynamicRestMapper,
config: config,
kc: kc,
templateClient: templateClient,
buildClient: buildClient,
lister: informer.Lister(),
informer: informer.Informer(),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "openshift_template_instance_controller"),
readinessLimiter: workqueue.NewItemFastSlowRateLimiter(5*time.Second, 20*time.Second, 200),
clock: clock.RealClock{},
}

c.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand All @@ -97,6 +101,9 @@ func NewTemplateInstanceController(config *rest.Config, kc kclientsetinternal.In
},
})

c.jsonConfig = rest.CopyConfig(c.config)
c.jsonConfig.ContentConfig = dynamic.ContentConfig()
Copy link
Contributor

Choose a reason for hiding this comment

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

this is suspicious. Still reading down


prometheus.MustRegister(c)

return c
Expand Down Expand Up @@ -215,7 +222,7 @@ func (c *TemplateInstanceController) checkReadiness(templateInstance *templateap
continue
}

mapping, err := c.restmapper.RESTMapping(object.Ref.GroupVersionKind().GroupKind())
mapping, err := c.dynamicRestMapper.RESTMapping(object.Ref.GroupVersionKind().GroupKind())
if err != nil {
return false, err
}
Expand Down Expand Up @@ -407,7 +414,7 @@ func (c *TemplateInstanceController) instantiate(templateInstance *templateapi.T
return err
}

errs := runtime.DecodeList(template.Objects, legacyscheme.Codecs.UniversalDecoder())
errs := runtime.DecodeList(template.Objects, unstructured.UnstructuredJSONScheme)
if len(errs) > 0 {
return kerrs.NewAggregate(errs)
}
Expand All @@ -424,19 +431,24 @@ func (c *TemplateInstanceController) instantiate(templateInstance *templateapi.T
}

for _, obj := range template.Objects {
meta, _ := meta.Accessor(obj)
meta, err := meta.Accessor(obj)
if err != nil {
return err
}
ref := meta.GetOwnerReferences()
ref = append(ref, templateInstanceOwnerRef)
meta.SetOwnerReferences(ref)
}

bulk := bulk.Bulk{
Mapper: &resource.Mapper{
RESTMapper: c.restmapper,
ObjectTyper: legacyscheme.Scheme,
ClientMapper: bulk.ClientMapperFromConfig(c.config),
DynamicMapper: &resource.Mapper{
RESTMapper: c.dynamicRestMapper,
ObjectTyper: discovery.NewUnstructuredObjectTyper(nil),
ClientMapper: bulk.ClientMapperFromConfig(c.jsonConfig),
},

Op: func(info *resource.Info, namespace string, obj runtime.Object) (runtime.Object, error) {

if len(info.Namespace) > 0 {
namespace = info.Namespace
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/template/controller/templateinstance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestControllerCheckReadiness(t *testing.T) {
// fakeclient, respond "allowed" to any subjectaccessreview
fakeclientset := &fake.Clientset{}
c := &TemplateInstanceController{
restmapper: restutil.DefaultMultiRESTMapper(),
kc: fakeclientset,
config: fakerestconfig,
clock: clock,
dynamicRestMapper: restutil.DefaultMultiRESTMapper(),
kc: fakeclientset,
config: fakerestconfig,
clock: clock,
}
fakeclientset.AddReactor("create", "subjectaccessreviews", func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &authorization.SubjectAccessReview{Status: authorization.SubjectAccessReviewStatus{Allowed: true}}, nil
Expand Down
16 changes: 15 additions & 1 deletion test/extended/testdata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion test/extended/testdata/templates/templateservicebroker_bind.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apiVersion: v1
kind: Template
metadata:
name: tsbtemplate
objects:
- apiVersion: v1
kind: Secret
Expand Down Expand Up @@ -47,7 +49,7 @@ objects:
ports:
- name: port
port: 1234
- apiVersion: v1
- apiVersion: route.openshift.io/v1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liggitt I think this template covers all the variants you were concerned w/ now:

  1. openshift api object by group (route)
  2. openshift api object by legacy (service)
  3. k8s api object by standard api (configmap)
  4. k8s api object by group (CRD)

Copy link
Contributor

Choose a reason for hiding this comment

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

openshift api object by legacy (service)

service is a kube object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah dunno what i was thinking. updated w/ a second route object that uses the legacy api.

kind: Route
metadata:
annotations:
Expand All @@ -59,6 +61,18 @@ objects:
to:
kind: Service
name: service
- apiVersion: v1
kind: Route
metadata:
annotations:
template.openshift.io/expose-route-uri2: http://{.spec.host}{.spec.path}
name: legacyroute
spec:
host: host
path: /path
to:
kind: Service
name: service

- apiVersion: template.openshift.io/v1
kind: BrokerTemplateInstance
Expand Down