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

switch process to produce groupified openshift api resources #19458

Merged
merged 2 commits into from
Apr 24, 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
3 changes: 2 additions & 1 deletion hack/import-restrictions.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
"github.com/openshift/origin/pkg/api/apihelpers",
"github.com/openshift/origin/pkg/api/install",
"github.com/openshift/origin/pkg/api/latest",
"github.com/openshift/origin/pkg/api/legacygroupification",
"github.com/openshift/origin/pkg/api/meta",
"github.com/openshift/origin/pkg/apps/apis/apps",
"github.com/openshift/origin/pkg/apps/apis/apps/install",
Expand Down Expand Up @@ -508,11 +509,11 @@
"github.com/openshift/origin/pkg/serviceaccounts",
"github.com/openshift/origin/pkg/serviceaccounts/util",
"github.com/openshift/origin/pkg/service/admission",
"github.com/openshift/origin/pkg/template",
"github.com/openshift/origin/pkg/template/apis/template",
"github.com/openshift/origin/pkg/template/apis/template/validation",
"github.com/openshift/origin/pkg/template/client/internalversion",
"github.com/openshift/origin/pkg/template/generator",
"github.com/openshift/origin/pkg/template/templateprocessing",
"github.com/openshift/origin/pkg/unidling/api",
"github.com/openshift/origin/pkg/unidling/util",
"github.com/openshift/origin/pkg/user/apis/user",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package legacygroupification

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/controller/jenkins/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/golang/glog"
"github.com/openshift/origin/pkg/template/templateprocessing"
"k8s.io/kubernetes/pkg/api/legacyscheme"

kerrs "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -13,7 +14,6 @@ import (
kapi "k8s.io/kubernetes/pkg/apis/core"

serverapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templateinternal "github.com/openshift/origin/pkg/template/client/internalversion"
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset"
Expand Down Expand Up @@ -104,7 +104,7 @@ func substituteTemplateParameters(params map[string]string, t *templateapi.Templ
errors = append(errors, fmt.Errorf("template parameter name cannot be empty (%q)", value))
continue
}
if v := template.GetParameterByName(t, name); v != nil {
if v := templateprocessing.GetParameterByName(t, name); v != nil {
v.Value = value
v.Generate = ""
} else {
Expand Down
5 changes: 3 additions & 2 deletions pkg/oc/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"strings"

"github.com/openshift/origin/pkg/api/legacygroupification"
"github.com/spf13/cobra"

kubecmd "k8s.io/kubernetes/pkg/kubectl/cmd"
Expand Down Expand Up @@ -317,8 +318,8 @@ func CommandFor(basename string) *cobra.Command {
cmd = kubecmd.NewKubectlCommand(kcmdutil.NewFactory(nil), in, out, errout)
default:
// we only need this change for `oc`. `kubectl` should behave as close to `kubectl` as we can
resource.OAPIToGroupified = OAPIToGroupified
kcmdutil.OAPIToGroupifiedGVK = OAPIToGroupifiedGVK
resource.OAPIToGroupified = legacygroupification.OAPIToGroupified
kcmdutil.OAPIToGroupifiedGVK = legacygroupification.OAPIToGroupifiedGVK
cmd = NewCommandCLI("oc", "oc", in, out, errout)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/oc/cli/cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/openshift/origin/pkg/template/templateprocessing"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/openshift/origin/pkg/oc/cli/describe"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
"github.com/openshift/origin/pkg/oc/generate/app"
"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templatevalidation "github.com/openshift/origin/pkg/template/apis/template/validation"
templateinternalclient "github.com/openshift/origin/pkg/template/client/internalversion"
Expand Down Expand Up @@ -363,7 +363,7 @@ func RunProcess(f *clientcmd.Factory, in io.Reader, out, errout io.Writer, cmd *
func injectUserVars(values app.Environment, t *templateapi.Template, ignoreUnknownParameters bool) []error {
var errors []error
for param, val := range values {
v := template.GetParameterByName(t, param)
v := templateprocessing.GetParameterByName(t, param)
if v != nil {
v.Value = val
v.Generate = ""
Expand All @@ -380,7 +380,7 @@ func processTemplateLocally(tpl *templateapi.Template) error {
if errs := templatevalidation.ValidateProcessedTemplate(tpl); len(errs) > 0 {
return errors.NewInvalid(templateapi.Kind("Template"), tpl.Name, errs)
}
processor := template.NewProcessor(map[string]generator.Generator{
processor := templateprocessing.NewProcessor(map[string]generator.Generator{
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))),
})
if errs := processor.Process(tpl); len(errs) > 0 {
Expand Down
43 changes: 40 additions & 3 deletions pkg/oc/generate/app/templatelookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/resource"

"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset/typed/template/internalversion"
)
Expand All @@ -34,7 +33,7 @@ func (r TemplateSearcher) Search(precise bool, terms ...string) (ComponentMatche
matches := ComponentMatches{}
var errs []error
for _, term := range terms {
ref, err := template.ParseTemplateReference(term)
ref, err := parseTemplateReference(term)
if err != nil {
glog.V(2).Infof("template references must be of the form [<namespace>/]<name>, term %q did not qualify", term)
continue
Expand All @@ -45,7 +44,7 @@ func (r TemplateSearcher) Search(precise bool, terms ...string) (ComponentMatche
}

namespaces := r.Namespaces
if ref.HasNamespace() {
if ref.hasNamespace() {
namespaces = []string{ref.Namespace}
}

Expand Down Expand Up @@ -182,3 +181,41 @@ func (r *TemplateFileSearcher) Search(precise bool, terms ...string) (ComponentM

return matches, errs
}

// templateReference points to a stored template
type templateReference struct {
Namespace string
Name string
}

// parseTemplateReference parses the reference to a template into a
// TemplateReference.
func parseTemplateReference(s string) (templateReference, error) {
var ref templateReference
parts := strings.Split(s, "/")
switch len(parts) {
case 2:
// namespace/name
ref.Namespace = parts[0]
ref.Name = parts[1]
break
case 1:
// name
ref.Name = parts[0]
break
default:
return ref, fmt.Errorf("the template reference must be either the template name or namespace and template name separated by slashes")
}
return ref, nil
}

func (r templateReference) hasNamespace() bool {
return len(r.Namespace) > 0
}

func (r templateReference) String() string {
if r.hasNamespace() {
return fmt.Sprintf("%s/%s", r.Namespace, r.Name)
}
return r.Name
}
4 changes: 2 additions & 2 deletions pkg/oc/generate/cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"io"
"strings"

"github.com/openshift/origin/pkg/template/templateprocessing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/kubernetes/pkg/api/legacyscheme"

"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/oc/generate/app"
"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templateinternalclient "github.com/openshift/origin/pkg/template/client/internalversion"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -21,7 +21,7 @@ import (
func TransformTemplate(tpl *templateapi.Template, templateProcessor templateinternalclient.TemplateProcessorInterface, namespace string, parameters map[string]string, ignoreUnknownParameters bool) (*templateapi.Template, error) {
// only set values that match what's expected by the template.
for k, value := range parameters {
v := template.GetParameterByName(tpl, k)
v := templateprocessing.GetParameterByName(tpl, k)
if v != nil {
v.Value = value
v.Generate = ""
Expand Down
3 changes: 0 additions & 3 deletions pkg/template/doc.go

This file was deleted.

87 changes: 0 additions & 87 deletions pkg/template/helper.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/template/registry/template/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"time"

"github.com/golang/glog"
"github.com/openshift/origin/pkg/template/templateprocessing"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"

"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templatevalidation "github.com/openshift/origin/pkg/template/apis/template/validation"
"github.com/openshift/origin/pkg/template/generator"
Expand Down Expand Up @@ -50,7 +50,7 @@ func (s *REST) Create(ctx apirequest.Context, obj runtime.Object, _ rest.Validat
generators := map[string]generator.Generator{
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))),
}
processor := template.NewProcessor(generators)
processor := templateprocessing.NewProcessor(generators)
if errs := processor.Process(tpl); len(errs) > 0 {
glog.V(1).Infof(errs.ToAggregate().Error())
return nil, errors.NewInvalid(templateapi.Kind("Template"), tpl.Name, errs)
Expand Down
24 changes: 22 additions & 2 deletions pkg/template/registry/templateinstance/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"

"github.com/openshift/origin/pkg/authorization/util"
template "github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
"github.com/openshift/origin/pkg/template/apis/template/validation"
)
Expand Down Expand Up @@ -60,7 +59,7 @@ func (templateInstanceStrategy) PrepareForCreate(ctx apirequest.Context, obj run
if templateInstance.Spec.Requester == nil {

if user, ok := apirequest.UserFrom(ctx); ok {
templateReq := template.ConvertUserToTemplateInstanceRequester(user)
templateReq := convertUserToTemplateInstanceRequester(user)
templateInstance.Spec.Requester = &templateReq
}
}
Expand Down Expand Up @@ -192,3 +191,24 @@ func (statusStrategy) Canonicalize(obj runtime.Object) {
func (statusStrategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateTemplateInstanceUpdate(obj.(*templateapi.TemplateInstance), old.(*templateapi.TemplateInstance))
}

// convertUserToTemplateInstanceRequester copies analogous fields from user.Info to TemplateInstanceRequester
func convertUserToTemplateInstanceRequester(u user.Info) templateapi.TemplateInstanceRequester {
templatereq := templateapi.TemplateInstanceRequester{}

if u != nil {
extra := map[string]templateapi.ExtraValue{}
if u.GetExtra() != nil {
for k, v := range u.GetExtra() {
extra[k] = templateapi.ExtraValue(v)
}
}

templatereq.Username = u.GetName()
templatereq.UID = u.GetUID()
templatereq.Groups = u.GetGroups()
templatereq.Extra = extra
}

return templatereq
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package template
package templateprocessing

import (
"fmt"
Expand All @@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/origin/pkg/api/legacygroupification"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
. "github.com/openshift/origin/pkg/template/generator"
"github.com/openshift/origin/pkg/util"
Expand Down Expand Up @@ -84,6 +85,11 @@ func (p *Processor) Process(template *templateapi.Template) field.ErrorList {
// referenced namespace.
stripNamespace(item)

// this changes oapi GVKs to groupified GVKs so they can be submitted to modern, aggregated servers
gvk := item.GetObjectKind().GroupVersionKind()
legacygroupification.OAPIToGroupifiedGVK(&gvk)
item.GetObjectKind().SetGroupVersionKind(gvk)

newItem, err := p.SubstituteParameters(paramMap, item)
if err != nil {
templateErrors = append(templateErrors, field.Invalid(idxPath.Child("parameters"), template.Parameters, err.Error()))
Expand Down
Loading