-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Configuration.pkg converters
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
373 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2023 Upbound Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package migration | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
const ( | ||
errEditConfigurationPackageFmt = `failed to put the edited Configuration package: %s` | ||
) | ||
|
||
func (pg *PlanGenerator) convertConfigurationPackage(o UnstructuredWithMetadata) error { | ||
pkg, err := toConfigurationPackageV1(o.Object) | ||
if err != nil { | ||
return err | ||
} | ||
for _, pkgConv := range pg.registry.configurationPackageConverters { | ||
if pkgConv.re == nil || pkgConv.converter == nil || !pkgConv.re.MatchString(pkg.Spec.Package) { | ||
continue | ||
} | ||
targetPkgs, err := pkgConv.converter.ConfigurationPackageV1(*pkg) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to call converter on Configuration package: %s", pkg.Spec.Package) | ||
} | ||
// TODO: if a converter only converts a specific version, | ||
// (or does not convert the given configuration), | ||
// we will have a false positive. Better to compute and check | ||
// a diff here. | ||
targets := make([]*UnstructuredWithMetadata, 0, len(targetPkgs)) | ||
for _, p := range targetPkgs { | ||
p := p | ||
targets = append(targets, &UnstructuredWithMetadata{ | ||
Object: ToSanitizedUnstructured(&p), | ||
Metadata: o.Metadata, | ||
}) | ||
} | ||
if err := pg.stepEditConfigurationPackage(o, targets); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (pg *PlanGenerator) stepEditConfigurationPackage(source UnstructuredWithMetadata, targets []*UnstructuredWithMetadata) error { | ||
s := pg.stepConfiguration(stepEditConfigurationPackage) | ||
for _, t := range targets { | ||
t.Metadata.Path = fmt.Sprintf("%s/%s.yaml", s.Name, getVersionedName(t.Object)) | ||
s.Patch.Files = append(s.Patch.Files, t.Metadata.Path) | ||
patchMap, err := computeJSONMergePathDoc(source.Object, t.Object) | ||
if err != nil { | ||
return err | ||
} | ||
return errors.Wrap(pg.target.Put(UnstructuredWithMetadata{ | ||
Object: unstructured.Unstructured{ | ||
Object: addNameGVK(t.Object, patchMap), | ||
}, | ||
Metadata: t.Metadata, | ||
}), errEditConfigurationPackageFmt) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.