Repository: kumahq/kuma
Pull request #1514 (2021-02-04 11:46:11)

feat(kuma-cp) inlineString in DataSource
### Summary

This PR introduces the capability to provide inline string in DataSource (used for example in ExternalService#tls)



Patch:
diff --git a/api/mesh/v1alpha1/dataplane.pb.go b/api/mesh/v1alpha1/dataplane.pb.go
index ce21b1eb7..b89786d15 100644
--- a/api/mesh/v1alpha1/dataplane.pb.go
+++ b/api/mesh/v1alpha1/dataplane.pb.go
@@ -744,6 +744,7 @@ func (x *Dataplane_Networking_Inbound_Health) GetReady() bool {
 	return false
 }
 
+// ServiceProbe defines parameters for probing service's port
 type Dataplane_Networking_Inbound_ServiceProbe struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
@@ -756,9 +757,11 @@ type Dataplane_Networking_Inbound_ServiceProbe struct {
 	// Number of consecutive unhealthy checks before considering a host
 	// unhealthy.
 	UnhealthyThreshold *wrappers.UInt32Value `protobuf:"bytes,3,opt,name=unhealthy_threshold,json=unhealthyThreshold,proto3" json:"unhealthy_threshold,omitempty"`
-	// Number of consecutive healthy checks before considering a host healthy.
-	HealthyThreshold *wrappers.UInt32Value                          `protobuf:"bytes,4,opt,name=healthy_threshold,json=healthyThreshold,proto3" json:"healthy_threshold,omitempty"`
-	Tcp              *Dataplane_Networking_Inbound_ServiceProbe_Tcp `protobuf:"bytes,5,opt,name=tcp,proto3" json:"tcp,omitempty"`
+	// Number of consecutive healthy checks before considering a host
+	// healthy.
+	HealthyThreshold *wrappers.UInt32Value `protobuf:"bytes,4,opt,name=healthy_threshold,json=healthyThreshold,proto3" json:"healthy_threshold,omitempty"`
+	// Tcp checker tries to establish tcp connection with destination
+	Tcp *Dataplane_Networking_Inbound_ServiceProbe_Tcp `protobuf:"bytes,5,opt,name=tcp,proto3" json:"tcp,omitempty"`
 }
 
 func (x *Dataplane_Networking_Inbound_ServiceProbe) Reset() {
diff --git a/api/system/v1alpha1/datasource.pb.go b/api/system/v1alpha1/datasource.pb.go
index 8726055b5..a617b1a0b 100644
--- a/api/system/v1alpha1/datasource.pb.go
+++ b/api/system/v1alpha1/datasource.pb.go
@@ -36,6 +36,7 @@ type DataSource struct {
 	//	*DataSource_Secret
 	//	*DataSource_File
 	//	*DataSource_Inline
+	//	*DataSource_InlineString
 	Type isDataSource_Type `protobuf_oneof:"type"`
 }
 
@@ -99,6 +100,13 @@ func (x *DataSource) GetInline() *wrappers.BytesValue {
 	return nil
 }
 
+func (x *DataSource) GetInlineString() string {
+	if x, ok := x.GetType().(*DataSource_InlineString); ok {
+		return x.InlineString
+	}
+	return ""
+}
+
 type isDataSource_Type interface {
 	isDataSource_Type()
 }
@@ -118,12 +126,19 @@ type DataSource_Inline struct {
 	Inline *wrappers.BytesValue `protobuf:"bytes,3,opt,name=inline,proto3,oneof"`
 }
 
+type DataSource_InlineString struct {
+	// Data source is inline string
+	InlineString string `protobuf:"bytes,4,opt,name=inlineString,proto3,oneof"`
+}
+
 func (*DataSource_Secret) isDataSource_Type() {}
 
 func (*DataSource_File) isDataSource_Type() {}
 
 func (*DataSource_Inline) isDataSource_Type() {}
 
+func (*DataSource_InlineString) isDataSource_Type() {}
+
 var File_system_v1alpha1_datasource_proto protoreflect.FileDescriptor
 
 var file_system_v1alpha1_datasource_proto_rawDesc = []byte{
@@ -132,18 +147,21 @@ var file_system_v1alpha1_datasource_proto_rawDesc = []byte{
 	0x74, 0x6f, 0x12, 0x14, 0x6b, 0x75, 0x6d, 0x61, 0x2e, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
 	0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
 	0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x77, 0x72, 0x61, 0x70, 0x70, 0x65,
-	0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x7b, 0x0a, 0x0a, 0x44, 0x61, 0x74, 0x61,
-	0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x18, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74,
-	0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74,
-	0x12, 0x14, 0x0a, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00,
-	0x52, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x12, 0x35, 0x0a, 0x06, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x65,
-	0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
-	0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x42, 0x79, 0x74, 0x65, 0x73, 0x56, 0x61,
-	0x6c, 0x75, 0x65, 0x48, 0x00, 0x52, 0x06, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x42, 0x06, 0x0a,
-	0x04, 0x74, 0x79, 0x70, 0x65, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e,
-	0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x75, 0x6d, 0x61, 0x68, 0x71, 0x2f, 0x6b, 0x75, 0x6d, 0x61, 0x2f,
-	0x61, 0x70, 0x69, 0x2f, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70,
-	0x68, 0x61, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa1, 0x01, 0x0a, 0x0a, 0x44, 0x61, 0x74,
+	0x61, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x18, 0x0a, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65,
+	0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65,
+	0x74, 0x12, 0x14, 0x0a, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48,
+	0x00, 0x52, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x12, 0x35, 0x0a, 0x06, 0x69, 0x6e, 0x6c, 0x69, 0x6e,
+	0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
+	0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x42, 0x79, 0x74, 0x65, 0x73, 0x56,
+	0x61, 0x6c, 0x75, 0x65, 0x48, 0x00, 0x52, 0x06, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x12, 0x24,
+	0x0a, 0x0c, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x04,
+	0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x0c, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x53, 0x74,
+	0x72, 0x69, 0x6e, 0x67, 0x42, 0x06, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x42, 0x2c, 0x5a, 0x2a,
+	0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x75, 0x6d, 0x61, 0x68,
+	0x71, 0x2f, 0x6b, 0x75, 0x6d, 0x61, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x73, 0x79, 0x73, 0x74, 0x65,
+	0x6d, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
+	0x6f, 0x33,
 }
 
 var (
@@ -195,6 +213,7 @@ func file_system_v1alpha1_datasource_proto_init() {
 		(*DataSource_Secret)(nil),
 		(*DataSource_File)(nil),
 		(*DataSource_Inline)(nil),
+		(*DataSource_InlineString)(nil),
 	}
 	type x struct{}
 	out := protoimpl.TypeBuilder{
diff --git a/api/system/v1alpha1/datasource.proto b/api/system/v1alpha1/datasource.proto
index 93e0929d3..e4c3df805 100644
--- a/api/system/v1alpha1/datasource.proto
+++ b/api/system/v1alpha1/datasource.proto
@@ -16,5 +16,7 @@ message DataSource {
     string file = 2;
     // Data source is inline bytes.
     google.protobuf.BytesValue inline = 3;
+    // Data source is inline string
+    string inlineString = 4;
   }
 }
diff --git a/pkg/core/datasource/loader.go b/pkg/core/datasource/loader.go
index 230c03b86..857f934be 100644
--- a/pkg/core/datasource/loader.go
+++ b/pkg/core/datasource/loader.go
@@ -37,6 +37,8 @@ func (l *loader) Load(ctx context.Context, mesh string, source *system_proto.Dat
 		data, err = l.loadSecret(ctx, mesh, source.GetSecret())
 	case *system_proto.DataSource_Inline:
 		data, err = source.GetInline().GetValue(), nil
+	case *system_proto.DataSource_InlineString:
+		data, err = []byte(source.GetInlineString()), nil
 	case *system_proto.DataSource_File:
 		data, err = ioutil.ReadFile(source.GetFile())
 	default:


Test patch:
diff --git a/pkg/core/datasource/loader_test.go b/pkg/core/datasource/loader_test.go
index 4ced4b7ba..78cb79288 100644
--- a/pkg/core/datasource/loader_test.go
+++ b/pkg/core/datasource/loader_test.go
@@ -120,4 +120,19 @@ var _ = Describe("DataSource Loader", func() {
 			Expect(data).To(Equal([]byte("abc")))
 		})
 	})
+
+	Context("Inline string", func() {
+		It("should load from inline string", func() {
+			// when
+			data, err := dataSourceLoader.Load(context.Background(), "default", &system_proto.DataSource{
+				Type: &system_proto.DataSource_InlineString{
+					InlineString: "abc",
+				},
+			})
+
+			// then
+			Expect(err).ToNot(HaveOccurred())
+			Expect(data).To(Equal([]byte("abc")))
+		})
+	})
 })