1// Copyright 2018 Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package config
16
17import (
18	"context"
19
20	"github.com/gogo/protobuf/proto"
21	"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
22	"github.com/gogo/protobuf/types"
23
24	adptTmpl "istio.io/api/mixer/adapter/model/v1beta1"
25	"istio.io/api/policy/v1beta1"
26	"istio.io/istio/mixer/pkg/adapter"
27	"istio.io/istio/mixer/pkg/protobuf/yaml/dynamic"
28	"istio.io/istio/mixer/pkg/runtime/lang"
29	"istio.io/istio/mixer/pkg/template"
30	"istio.io/pkg/attribute"
31)
32
33type (
34
35	// Snapshot view of configuration.
36	Snapshot struct {
37		ID int64
38
39		// Static information
40		Templates map[string]*template.Info
41		Adapters  map[string]*adapter.Info
42
43		// Config store based information
44		Attributes attribute.AttributeDescriptorFinder
45
46		HandlersStatic  map[string]*HandlerStatic
47		InstancesStatic map[string]*InstanceStatic
48
49		//  TemplateMetadatas contains template descriptors loaded from the store
50		TemplateMetadatas map[string]*Template
51		//  AdapterMetadatas contains adapter metadata loaded from the store
52		AdapterMetadatas map[string]*Adapter
53
54		HandlersDynamic  map[string]*HandlerDynamic
55		InstancesDynamic map[string]*InstanceDynamic
56		Rules            []*Rule
57
58		// Used to update Perf measures relevant to configuration.
59		MonitoringContext context.Context
60	}
61
62	// HandlerDynamic configuration for dynamically loaded, grpc adapters. Fully resolved.
63	HandlerDynamic struct {
64		Name string
65
66		Adapter *Adapter
67
68		// AdapterConfig used to construct the Handler. This is passed in verbatim to the remote adapter.
69		AdapterConfig *types.Any
70
71		// Connection information for the handler.
72		Connection *v1beta1.Connection
73	}
74
75	// HandlerStatic configuration for compiled in adapters. Fully resolved.
76	HandlerStatic struct {
77
78		// Name of the Handler. Fully qualified.
79		Name string
80
81		// Associated adapter. Always resolved.
82		Adapter *adapter.Info
83
84		// parameters used to construct the Handler.
85		Params proto.Message
86	}
87
88	// InstanceDynamic configuration for dynamically loaded templates. Fully resolved.
89	InstanceDynamic struct {
90		Name string
91
92		Template *Template
93
94		// Encoder to create request instance bytes from attributes
95		Encoder dynamic.Encoder
96
97		// Params of the instance; used to to create the config SHA.
98		Params map[string]interface{}
99
100		// AttributeBindings used to map the adapter output back into attributes
101		AttributeBindings map[string]string
102
103		// Language runtime to use for output expressions
104		Language lang.LanguageRuntime
105	}
106
107	// InstanceStatic configuration for compiled templates. Fully resolved.
108	InstanceStatic struct {
109		// Name of the instance. Fully qualified.
110		Name string
111
112		// Associated template. Always resolved.
113		Template *template.Info
114
115		// parameters used to construct the instance.
116		Params proto.Message
117
118		// inferred type for the instance.
119		InferredType proto.Message
120
121		// Language runtime to use for output expressions
122		Language lang.LanguageRuntime
123	}
124
125	// Rule configuration. Fully resolved.
126	Rule struct {
127		// Name of the rule
128		Name string
129
130		// Namespace of the rule
131		Namespace string
132
133		// Match condition
134		Match string
135
136		ActionsDynamic []*ActionDynamic
137
138		ActionsStatic []*ActionStatic
139
140		RequestHeaderOperations []*v1beta1.Rule_HeaderOperationTemplate
141
142		ResponseHeaderOperations []*v1beta1.Rule_HeaderOperationTemplate
143
144		// Language runtime to use for expressions
145		Language lang.LanguageRuntime
146	}
147
148	// ActionDynamic configuration. Fully resolved.
149	ActionDynamic struct {
150		// Handler that this action is resolved to.
151		Handler *HandlerDynamic
152		// Instances that should be generated as part of invoking action.
153		Instances []*InstanceDynamic
154		// Name of the action (optional)
155		Name string
156	}
157
158	// ActionStatic configuration. Fully resolved.
159	ActionStatic struct {
160		// Handler that this action is resolved to.
161		Handler *HandlerStatic
162		// Instances that should be generated as part of invoking action.
163		Instances []*InstanceStatic
164		// Name of the action (optional)
165		Name string
166	}
167
168	// Template contains info about a template
169	Template struct {
170		// Name of the template.
171		//
172		// Note this is the template's resource name and not the template's internal name that adapter developer
173		// uses to implement adapter service.
174		Name string
175
176		// Variety of this template
177		Variety adptTmpl.TemplateVariety
178
179		// InternalPackageDerivedName is the name of the template from adapter developer point of view.
180		// The service and functions implemented by the adapter is based on this name
181		// NOTE: This name derived from template proto package and not the resource name.
182		InternalPackageDerivedName string
183
184		// Template's file descriptor set.
185		FileDescSet *descriptor.FileDescriptorSet
186
187		// package name of the `Template` message
188		PackageName string
189
190		// AttributeManifest declares the output attributes for the template.
191		// For attribute producing adapters, the output attributes are of the form $out.field_name.
192		AttributeManifest map[string]*v1beta1.AttributeManifest_AttributeInfo
193	}
194
195	// Adapter contains info about an adapter
196	Adapter struct {
197		Name string
198
199		// Adapter's file descriptor set.
200		ConfigDescSet *descriptor.FileDescriptorSet
201
202		// package name of the `Params` message
203		PackageName string
204
205		SupportedTemplates []*Template
206
207		SessionBased bool
208
209		Description string
210	}
211)
212
213// Empty returns a new, empty configuration snapshot.
214func Empty() *Snapshot {
215	return &Snapshot{
216		ID:                -1,
217		Rules:             []*Rule{},
218		MonitoringContext: context.Background(),
219	}
220}
221
222// GetName gets name
223func (h HandlerStatic) GetName() string {
224	return h.Name
225}
226
227// AdapterName gets adapter name
228func (h HandlerStatic) AdapterName() string {
229	return h.Adapter.Name
230}
231
232// AdapterParams gets AdapterParams
233func (h HandlerStatic) AdapterParams() interface{} {
234	return h.Params
235}
236
237// ConnectionConfig returns nil for static handler
238func (h HandlerStatic) ConnectionConfig() interface{} {
239	return nil
240}
241
242// GetName gets name
243func (i InstanceStatic) GetName() string {
244	return i.Name
245}
246
247// TemplateName gets TemplateName
248func (i InstanceStatic) TemplateName() string {
249	return i.Template.Name
250}
251
252// TemplateParams gets TemplateParams
253func (i InstanceStatic) TemplateParams() interface{} {
254	return i.Params
255}
256
257// GetName gets name
258func (h HandlerDynamic) GetName() string {
259	return h.Name
260}
261
262// AdapterName gets adapter name
263func (h HandlerDynamic) AdapterName() string {
264	return h.Adapter.Name
265}
266
267// AdapterParams gets AdapterParams
268func (h HandlerDynamic) AdapterParams() interface{} {
269	return h.AdapterConfig
270}
271
272// ConnectionConfig gets connection config of dynamic handler
273func (h HandlerDynamic) ConnectionConfig() interface{} {
274	return h.Connection
275}
276
277// GetName gets name
278func (i InstanceDynamic) GetName() string {
279	return i.Name
280}
281
282// TemplateName gets TemplateName
283func (i InstanceDynamic) TemplateName() string {
284	return i.Template.Name
285}
286
287// TemplateParams gets TemplateParams
288func (i InstanceDynamic) TemplateParams() interface{} {
289	return i.Params
290}
291