Skip to content

Commit

Permalink
mixer: add support for standard CRDs for compiled-in adapters (istio#…
Browse files Browse the repository at this point in the history
…12815)

* cherry pick subset of istio#12689

Signed-off-by: Kuat Yessenov <kuat@google.com>

* add support for compiled in adapters

Signed-off-by: Kuat Yessenov <kuat@google.com>

* patch log line

Signed-off-by: Kuat Yessenov <kuat@google.com>
  • Loading branch information
kyessenov authored and Joshua Blatt committed Apr 8, 2019
1 parent 7e813b4 commit 25eedac
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 7 deletions.
95 changes: 95 additions & 0 deletions mixer/pkg/runtime/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,100 @@ Attributes:
`,
wantErr: "instance='i1.instance.default'.template: template 'not.a.template' not found",
},

{
Name: "add static instance - missing template",
Events1: []*store.Event{
updateEvent("i1.instance.default", &descriptorpb.Instance{
CompiledTemplate: "checkk",
Params: &types.Struct{
Fields: map[string]*types.Value{
"extra_field": &types.Value{},
},
},
}),
},
E: `
ID: 0
TemplatesStatic:
Name: apa
Name: check
Name: quota
Name: report
AdaptersStatic:
Name: adapter1
Name: adapter2
HandlersStatic:
InstancesStatic:
Rules:
Attributes:
template.attr: BOOL
`,
wantErr: "instance='i1.instance.default': missing compiled template",
},
{
Name: "add static instance - bad params",
Events1: []*store.Event{
updateEvent("i1.instance.default", &descriptorpb.Instance{
CompiledTemplate: "check",
Params: &types.Struct{
Fields: map[string]*types.Value{
"extra_field": &types.Value{},
},
},
AttributeBindings: map[string]string{"test": "test"},
}),
},
E: `
ID: 0
TemplatesStatic:
Name: apa
Name: check
Name: quota
Name: report
AdaptersStatic:
Name: adapter1
Name: adapter2
HandlersStatic:
InstancesStatic:
Rules:
Attributes:
template.attr: BOOL
`,
wantErr: "instance='i1.instance.default': nil Value",
},

{
Name: "add static instance - extra field",
Events1: []*store.Event{
updateEvent("i1.instance.default", &descriptorpb.Instance{
CompiledTemplate: "check",
Params: &types.Struct{
Fields: map[string]*types.Value{
"extra_field": &types.Value{Kind: &types.Value_StringValue{StringValue: "test"}},
},
},
}),
},
E: `
ID: 0
TemplatesStatic:
Name: apa
Name: check
Name: quota
Name: report
AdaptersStatic:
Name: adapter1
Name: adapter2
HandlersStatic:
InstancesStatic:
Rules:
Attributes:
template.attr: BOOL
`,
wantErr: `instance='i1.instance.default': unknown field "extra_field" in v1beta1.Instance`,
},

{
Name: "add instance - bad param type",
Events1: []*store.Event{
Expand Down Expand Up @@ -4188,6 +4282,7 @@ var stdTemplates = map[string]*template.Info{
},
"check": {
Name: "check",
CtrCfg: &descriptorpb.Instance{},
Variety: adapter_model.TEMPLATE_VARIETY_CHECK,
InferType: func(cp proto.Message, tEvalFn template.TypeEvalFn) (proto.Message, error) {
return nil, nil
Expand Down
63 changes: 56 additions & 7 deletions mixer/pkg/runtime/config/ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ func (e *Ephemeral) processDynamicInstanceConfigs(ctx context.Context, templates
}

inst := resource.Spec.(*config.Instance)

// should be processed as static instance
if inst.CompiledTemplate != "" {
continue
}

instanceName := key.String()
log.Debugf("Processing incoming instance config: name='%s'\n%s", instanceName, resource.Spec)

Expand Down Expand Up @@ -470,15 +476,58 @@ func (e *Ephemeral) processInstanceConfigs(ctx context.Context, attributes ast.A
for key, resource := range e.entries {
var info *template.Info
var found bool
if info, found = e.templates[key.Kind]; !found {
// This config resource is not for an instance (or at least not for one that Mixer is currently aware of).
continue
}

var params proto.Message
instanceName := key.String()

log.Debugf("Processing incoming instance config: name='%s'\n%s", instanceName, resource.Spec)
inferredType, err := info.InferType(resource.Spec, func(s string) (config.ValueType, error) {
if key.Kind == constant.InstanceKind {
inst := resource.Spec.(*config.Instance)
if inst.CompiledTemplate == "" {
continue
}
info, found = e.templates[inst.CompiledTemplate]
if !found {
appendErr(ctx, errs, fmt.Sprintf("instance='%s'", instanceName), monitoring.InstanceErrs, "missing compiled template")
continue
}

// cast from struct to template specific proto
params = proto.Clone(info.CtrCfg)
buf := &bytes.Buffer{}

if inst.Params == nil {
inst.Params = &types.Struct{Fields: make(map[string]*types.Value)}
}

// populate attribute bindings
if len(inst.AttributeBindings) > 0 {
bindings := &types.Struct{Fields: make(map[string]*types.Value)}
for k, v := range inst.AttributeBindings {
bindings.Fields[k] = &types.Value{Kind: &types.Value_StringValue{StringValue: v}}
}
inst.Params.Fields["attribute_bindings"] = &types.Value{
Kind: &types.Value_StructValue{StructValue: bindings},
}
}
if err := (&jsonpb.Marshaler{}).Marshal(buf, inst.Params); err != nil {
appendErr(ctx, errs, fmt.Sprintf("instance='%s'", instanceName), monitoring.InstanceErrs, err.Error())
continue
}
if err := (&jsonpb.Unmarshaler{AllowUnknownFields: false}).Unmarshal(buf, params); err != nil {
appendErr(ctx, errs, fmt.Sprintf("instance='%s'", instanceName), monitoring.InstanceErrs, err.Error())
continue
}
} else {
info, found = e.templates[key.Kind]
if !found {
// This config resource is not for an instance (or at least not for one that Mixer is currently aware of).
continue
}
params = resource.Spec
}

log.Debugf("Processing incoming instance config: name='%s'\n%s", instanceName, params)
inferredType, err := info.InferType(params, func(s string) (config.ValueType, error) {
return e.tc.EvalType(s, attributes)
})
if err != nil {
Expand All @@ -488,7 +537,7 @@ func (e *Ephemeral) processInstanceConfigs(ctx context.Context, attributes ast.A
cfg := &InstanceStatic{
Name: instanceName,
Template: info,
Params: resource.Spec,
Params: params,
InferredType: inferredType,
Language: lang.GetLanguageRuntime(resource.Metadata.Annotations),
}
Expand Down

0 comments on commit 25eedac

Please sign in to comment.