From 25eedac3845b52a523234c74240a5e6ce91c462e Mon Sep 17 00:00:00 2001 From: Kuat Date: Mon, 8 Apr 2019 15:29:24 -0700 Subject: [PATCH] mixer: add support for standard CRDs for compiled-in adapters (#12815) * cherry pick subset of https://github.com/istio/istio/pull/12689/ Signed-off-by: Kuat Yessenov * add support for compiled in adapters Signed-off-by: Kuat Yessenov * patch log line Signed-off-by: Kuat Yessenov --- mixer/pkg/runtime/config/config_test.go | 95 +++++++++++++++++++++++++ mixer/pkg/runtime/config/ephemeral.go | 63 ++++++++++++++-- 2 files changed, 151 insertions(+), 7 deletions(-) diff --git a/mixer/pkg/runtime/config/config_test.go b/mixer/pkg/runtime/config/config_test.go index dd38a7be168f..51a2e7838914 100644 --- a/mixer/pkg/runtime/config/config_test.go +++ b/mixer/pkg/runtime/config/config_test.go @@ -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{ @@ -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 diff --git a/mixer/pkg/runtime/config/ephemeral.go b/mixer/pkg/runtime/config/ephemeral.go index 64ad2ce2b400..618d826f40c9 100644 --- a/mixer/pkg/runtime/config/ephemeral.go +++ b/mixer/pkg/runtime/config/ephemeral.go @@ -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) @@ -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 { @@ -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), }