github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/service/environment.go (about) 1 /*This file is part of kuberpult. 2 3 Kuberpult is free software: you can redistribute it and/or modify 4 it under the terms of the Expat(MIT) License as published by 5 the Free Software Foundation. 6 7 Kuberpult is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 MIT License for more details. 11 12 You should have received a copy of the MIT License 13 along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. 14 15 Copyright 2023 freiheit.com*/ 16 17 package service 18 19 import ( 20 "context" 21 22 api "github.com/freiheit-com/kuberpult/pkg/api/v1" 23 24 "github.com/freiheit-com/kuberpult/services/cd-service/pkg/config" 25 "github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository" 26 ) 27 28 type EnvironmentServiceServer struct { 29 Repository repository.Repository 30 } 31 32 func (o *EnvironmentServiceServer) GetEnvironmentConfig( 33 ctx context.Context, 34 in *api.GetEnvironmentConfigRequest) (*api.GetEnvironmentConfigResponse, error) { 35 state := o.Repository.State() 36 config, err := state.GetEnvironmentConfig(in.Environment) 37 if err != nil { 38 return nil, err 39 } 40 var out api.GetEnvironmentConfigResponse 41 out.Config = TransformEnvironmentConfigToApi(*config) 42 return &out, nil 43 } 44 45 func TransformEnvironmentConfigToApi(in config.EnvironmentConfig) *api.EnvironmentConfig { 46 return &api.EnvironmentConfig{ 47 Upstream: transformUpstreamToApi(in.Upstream), 48 Argocd: transformArgoCdToApi(in.ArgoCd), 49 EnvironmentGroup: in.EnvironmentGroup, 50 } 51 } 52 53 func transformUpstreamToConfig(upstream *api.EnvironmentConfig_Upstream) *config.EnvironmentConfigUpstream { 54 if upstream == nil { 55 return nil 56 } 57 if upstream.GetLatest() { 58 return &config.EnvironmentConfigUpstream{ 59 Environment: "", 60 Latest: true, 61 } 62 } 63 if upstream.GetEnvironment() != "" { 64 return &config.EnvironmentConfigUpstream{ 65 Latest: false, 66 Environment: upstream.GetEnvironment(), 67 } 68 } 69 return nil 70 } 71 72 func transformUpstreamToApi(in *config.EnvironmentConfigUpstream) *api.EnvironmentConfig_Upstream { 73 if in == nil { 74 return nil 75 } 76 return &api.EnvironmentConfig_Upstream{ 77 Environment: &in.Environment, 78 Latest: &in.Latest, 79 } 80 } 81 82 func transformArgoCdToApi(in *config.EnvironmentConfigArgoCd) *api.EnvironmentConfig_ArgoCD { 83 if in == nil { 84 return nil 85 } 86 return &api.EnvironmentConfig_ArgoCD{ 87 ApplicationAnnotations: nil, 88 IgnoreDifferences: nil, 89 SyncOptions: nil, 90 SyncWindows: transformSyncWindowsToApi(in.SyncWindows), 91 Destination: transformDestinationToApi(&in.Destination), 92 AccessList: transformAccessEntryToApi(in.ClusterResourceWhitelist), 93 } 94 } 95 96 func transformSyncWindowsToConfig(syncWindows []*api.EnvironmentConfig_ArgoCD_SyncWindows) []config.ArgoCdSyncWindow { 97 var transformedSyncWindows []config.ArgoCdSyncWindow 98 for _, syncWindow := range syncWindows { 99 transformedSyncWindows = append(transformedSyncWindows, config.ArgoCdSyncWindow{ 100 Schedule: syncWindow.Schedule, 101 Duration: syncWindow.Duration, 102 Kind: syncWindow.Kind, 103 Apps: syncWindow.Applications, 104 }) 105 } 106 return transformedSyncWindows 107 } 108 109 func transformSyncWindowsToApi(in []config.ArgoCdSyncWindow) []*api.EnvironmentConfig_ArgoCD_SyncWindows { 110 var out []*api.EnvironmentConfig_ArgoCD_SyncWindows 111 for _, syncWindow := range in { 112 out = append(out, &api.EnvironmentConfig_ArgoCD_SyncWindows{ 113 Applications: nil, 114 Kind: syncWindow.Kind, 115 Schedule: syncWindow.Schedule, 116 Duration: syncWindow.Duration, 117 }) 118 } 119 return out 120 } 121 122 func transformAccessListToConfig(accessList []*api.EnvironmentConfig_ArgoCD_AccessEntry) []config.AccessEntry { 123 var transformedAccessList []config.AccessEntry 124 for _, accessEntry := range accessList { 125 transformedAccessList = append(transformedAccessList, config.AccessEntry{ 126 Group: accessEntry.Group, 127 Kind: accessEntry.Kind, 128 }) 129 } 130 return transformedAccessList 131 } 132 133 func transformAccessEntryToApi(in []config.AccessEntry) []*api.EnvironmentConfig_ArgoCD_AccessEntry { 134 var out []*api.EnvironmentConfig_ArgoCD_AccessEntry 135 for _, accessEntry := range in { 136 out = append(out, &api.EnvironmentConfig_ArgoCD_AccessEntry{ 137 Group: accessEntry.Group, 138 Kind: accessEntry.Kind, 139 }) 140 } 141 return out 142 } 143 144 func transformIgnoreDifferencesToConfig(ignoreDifferences []*api.EnvironmentConfig_ArgoCD_IgnoreDifferences) []config.ArgoCdIgnoreDifference { 145 var transformedIgnoreDifferences []config.ArgoCdIgnoreDifference 146 for _, ignoreDifference := range ignoreDifferences { 147 transformedIgnoreDifferences = append(transformedIgnoreDifferences, config.ArgoCdIgnoreDifference{ 148 Group: ignoreDifference.Group, 149 Kind: ignoreDifference.Kind, 150 Name: ignoreDifference.Name, 151 Namespace: ignoreDifference.Namespace, 152 JSONPointers: ignoreDifference.JsonPointers, 153 JqPathExpressions: ignoreDifference.JqPathExpressions, 154 ManagedFieldsManagers: ignoreDifference.ManagedFieldsManagers, 155 }) 156 } 157 return transformedIgnoreDifferences 158 } 159 160 func transformDestinationToConfig(in *api.EnvironmentConfig_ArgoCD_Destination) config.ArgoCdDestination { 161 if in == nil { 162 //exhaustruct:ignore 163 return config.ArgoCdDestination{} 164 } 165 return config.ArgoCdDestination{ 166 Name: in.Name, 167 Server: in.Server, 168 Namespace: in.Namespace, 169 AppProjectNamespace: in.AppProjectNamespace, 170 ApplicationNamespace: in.ApplicationNamespace, 171 } 172 } 173 174 func transformDestinationToApi(in *config.ArgoCdDestination) *api.EnvironmentConfig_ArgoCD_Destination { 175 if in == nil { 176 //exhaustruct:ignore 177 return &api.EnvironmentConfig_ArgoCD_Destination{} 178 } 179 return &api.EnvironmentConfig_ArgoCD_Destination{ 180 Name: in.Name, 181 Server: in.Server, 182 Namespace: in.Namespace, 183 AppProjectNamespace: in.AppProjectNamespace, 184 ApplicationNamespace: in.ApplicationNamespace, 185 } 186 }