1- package  v2 
1+ package  v3 
22
33import  (
44	"context" 
55	"errors" 
66	"fmt" 
77
8- 	v2  "github.com/cloudogu/k8s-blueprint-lib/v2 /api/v2 " 
9- 	serializerv2 "github.com/cloudogu/k8s-blueprint-operator/v2/pkg/adapter/kubernetes/blueprintcr/v2 /serializer" 
8+ 	bpv3  "github.com/cloudogu/k8s-blueprint-lib/v3 /api/v3 " 
9+ 	serializerv2 "github.com/cloudogu/k8s-blueprint-operator/v2/pkg/adapter/kubernetes/blueprintcr/v3 /serializer" 
1010	corev1 "k8s.io/api/core/v1" 
1111	k8sErrors "k8s.io/apimachinery/pkg/api/errors" 
1212	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
1313	"k8s.io/utils/ptr" 
1414	"sigs.k8s.io/controller-runtime/pkg/log" 
1515
16- 	bpv2client  "github.com/cloudogu/k8s-blueprint-lib/v2 /client" 
16+ 	bpv3client  "github.com/cloudogu/k8s-blueprint-lib/v3 /client" 
1717	"github.com/cloudogu/k8s-blueprint-operator/v2/pkg/domain" 
1818	"github.com/cloudogu/k8s-blueprint-operator/v2/pkg/domainservice" 
1919)
@@ -25,18 +25,21 @@ type blueprintSpecRepoContext struct {
2525}
2626
2727type  blueprintSpecRepo  struct  {
28- 	blueprintClient  blueprintInterface 
29- 	eventRecorder    eventRecorder 
28+ 	blueprintClient      blueprintInterface 
29+ 	blueprintMaskClient  blueprintMaskInterface 
30+ 	eventRecorder        eventRecorder 
3031}
3132
3233// NewBlueprintSpecRepository returns a new BlueprintSpecRepository to interact on BlueprintSpecs. 
3334func  NewBlueprintSpecRepository (
34- 	blueprintClient  bpv2client.BlueprintInterface ,
35+ 	blueprintClient  bpv3client.BlueprintInterface ,
36+ 	blueprintMaskClient  bpv3client.BlueprintMaskInterface ,
3537	eventRecorder  eventRecorder ,
3638) domainservice.BlueprintSpecRepository  {
3739	return  & blueprintSpecRepo {
38- 		blueprintClient : blueprintClient ,
39- 		eventRecorder :   eventRecorder ,
40+ 		blueprintClient :     blueprintClient ,
41+ 		blueprintMaskClient : blueprintMaskClient ,
42+ 		eventRecorder :       eventRecorder ,
4043	}
4144}
4245
@@ -79,7 +82,12 @@ func (repo *blueprintSpecRepo) GetById(ctx context.Context, blueprintId string)
7982		},
8083	}
8184
82- 	err  =  serializerv2 .SerializeBlueprintAndMask (blueprintSpec , blueprintCR )
85+ 	maskManifest , err  :=  repo .getMaskManifest (ctx , blueprintId , blueprintCR )
86+ 	if  err  !=  nil  {
87+ 		return  nil , err 
88+ 	}
89+ 
90+ 	err  =  serializerv2 .SerializeBlueprintAndMask (blueprintSpec , blueprintCR .Spec .Blueprint , maskManifest )
8391	if  err  !=  nil  {
8492		invalidErrorEvent  :=  domain.BlueprintSpecInvalidEvent {ValidationError : err }
8593		repo .eventRecorder .Event (blueprintCR , corev1 .EventTypeWarning , invalidErrorEvent .Name (), invalidErrorEvent .Message ())
@@ -90,6 +98,30 @@ func (repo *blueprintSpecRepo) GetById(ctx context.Context, blueprintId string)
9098	return  blueprintSpec , nil 
9199}
92100
101+ func  (repo  * blueprintSpecRepo ) getMaskManifest (ctx  context.Context , blueprintId  string , blueprintCR  * bpv3.Blueprint ) (* bpv3.BlueprintMaskManifest , error ) {
102+ 	if  blueprintCR .Spec .MaskSource .Manifest  !=  nil  &&  blueprintCR .Spec .MaskSource .CrRef  !=  nil  {
103+ 		err  :=  & domain.InvalidBlueprintError {Message : "blueprint mask and mask ref cannot be set at the same time" }
104+ 		invalidErrorEvent  :=  domain.BlueprintSpecInvalidEvent {ValidationError : err }
105+ 		repo .eventRecorder .Event (blueprintCR , corev1 .EventTypeWarning , invalidErrorEvent .Name (), invalidErrorEvent .Message ())
106+ 		return  nil , fmt .Errorf ("could not deserialize blueprint CR %q: %w" , blueprintId , err )
107+ 	}
108+ 
109+ 	var  maskManifest  =  blueprintCR .Spec .MaskSource .Manifest 
110+ 	if  blueprintCR .Spec .MaskSource .CrRef  !=  nil  {
111+ 		blueprintMask , maskErr  :=  repo .blueprintMaskClient .Get (ctx , blueprintCR .Spec .MaskSource .CrRef .Name , metav1.GetOptions {})
112+ 		if  maskErr  !=  nil  {
113+ 			return  nil , & domainservice.NotFoundError {
114+ 				WrappedError : maskErr ,
115+ 				Message :      fmt .Sprintf ("could not get blueprint mask from ref %q in blueprint %q" , blueprintCR .Spec .MaskSource .CrRef .Name , blueprintId ),
116+ 				DoNotRetry :   false ,
117+ 			}
118+ 		}
119+ 
120+ 		maskManifest  =  blueprintMask .Spec .BlueprintMaskManifest 
121+ 	}
122+ 	return  maskManifest , nil 
123+ }
124+ 
93125func  (repo  * blueprintSpecRepo ) Count (ctx  context.Context , limit  int ) (int , error ) {
94126	limit64  :=  int64 (limit )
95127
@@ -135,13 +167,13 @@ func (repo *blueprintSpecRepo) Update(ctx context.Context, spec *domain.Blueprin
135167
136168	effectiveBlueprint  :=  serializerv2 .ConvertToBlueprintDTO (spec .EffectiveBlueprint )
137169
138- 	updatedBlueprint  :=  & v2 .Blueprint {
170+ 	updatedBlueprint  :=  & bpv3 .Blueprint {
139171		ObjectMeta : metav1.ObjectMeta {
140172			Name :              spec .Id ,
141173			ResourceVersion :   persistenceContext .resourceVersion ,
142174			CreationTimestamp : metav1.Time {},
143175		},
144- 		Status : & v2 .BlueprintStatus {
176+ 		Status : & bpv3 .BlueprintStatus {
145177			EffectiveBlueprint : & effectiveBlueprint ,
146178			StateDiff :          serializerv2 .ConvertToStateDiffDTO (spec .StateDiff ),
147179			Conditions :         spec .Conditions ,
@@ -165,7 +197,7 @@ func (repo *blueprintSpecRepo) Update(ctx context.Context, spec *domain.Blueprin
165197	return  nil 
166198}
167199
168- func  setPersistenceContext (blueprintCR  * v2 .Blueprint , spec  * domain.BlueprintSpec ) {
200+ func  setPersistenceContext (blueprintCR  * bpv3 .Blueprint , spec  * domain.BlueprintSpec ) {
169201	if  spec .PersistenceContext  ==  nil  {
170202		spec .PersistenceContext  =  make (map [string ]interface {}, 1 )
171203	}
@@ -195,7 +227,7 @@ func getPersistenceContext(ctx context.Context, spec *domain.BlueprintSpec) (blu
195227	}
196228}
197229
198- func  (repo  * blueprintSpecRepo ) publishEvents (blueprintCR  * v2 .Blueprint , events  []domain.Event ) {
230+ func  (repo  * blueprintSpecRepo ) publishEvents (blueprintCR  * bpv3 .Blueprint , events  []domain.Event ) {
199231	for  _ , event  :=  range  events  {
200232		repo .eventRecorder .Event (blueprintCR , corev1 .EventTypeNormal , event .Name (), event .Message ())
201233	}
0 commit comments