-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update configconstraint update (#7382)
- Loading branch information
Showing
11 changed files
with
410 additions
and
60 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,91 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
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 hook | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Conversion struct { | ||
BasedHandler | ||
} | ||
|
||
type ConversionHandler interface { | ||
Convert(context.Context, CRClient) ([]client.Object, error) | ||
} | ||
|
||
type ConversionMeta struct { | ||
ConversionHandler | ||
FromVersion Version | ||
ToVersion Version | ||
} | ||
|
||
var crConversionMaps = map[schema.GroupVersionResource]ConversionMeta{} | ||
|
||
func (p *Conversion) IsSkip(ctx *UpgradeContext) (bool, error) { | ||
fromVersion := ctx.From | ||
toVersion := ctx.To | ||
|
||
if fromVersion == nil || *fromVersion == toVersion { | ||
Log("not support resource conversion and pass, oldVersion: %v, newVersion: %v", fromVersion, toVersion) | ||
return true, nil | ||
} | ||
|
||
for _, meta := range crConversionMaps { | ||
if matchVersion(meta, *fromVersion, toVersion) && meta.ConversionHandler != nil { | ||
return false, nil | ||
} | ||
} | ||
Log("no gvr to convert and pass, oldVersion: %v, newVersion: %v", fromVersion, toVersion) | ||
return true, nil | ||
} | ||
|
||
func (p *Conversion) Handle(ctx *UpgradeContext) (err error) { | ||
var ( | ||
fromVersion = ctx.From | ||
toVersion = ctx.To | ||
) | ||
|
||
for gvr, meta := range crConversionMaps { | ||
if !matchVersion(meta, *fromVersion, toVersion) || meta.ConversionHandler == nil { | ||
continue | ||
} | ||
objs, err := meta.Convert(ctx, ctx.CRClient) | ||
if err != nil { | ||
return err | ||
} | ||
if len(objs) != 0 { | ||
ctx.UpdatedObjects[gvr] = append(ctx.UpdatedObjects[gvr], objs...) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func matchVersion(meta ConversionMeta, oldVersion Version, newVersion Version) bool { | ||
return meta.FromVersion == oldVersion && meta.ToVersion == newVersion | ||
} | ||
|
||
func RegisterCRDConversion(oldVersion Version, newVersion Version, gvr schema.GroupVersionResource, handler ConversionHandler) { | ||
crConversionMaps[gvr] = ConversionMeta{ | ||
FromVersion: oldVersion, | ||
ToVersion: newVersion, | ||
ConversionHandler: handler, | ||
} | ||
} |
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,59 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
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 hook | ||
|
||
import ( | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
apiruntime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
type UpdateCR struct { | ||
BasedHandler | ||
} | ||
|
||
func (u *UpdateCR) Handle(context *UpgradeContext) error { | ||
if len(context.UpdatedObjects) == 0 { | ||
return nil | ||
} | ||
|
||
for gvr, objects := range context.UpdatedObjects { | ||
Log("update GVR resource: %s", gvr.String()) | ||
for _, cr := range objects { | ||
Log("update resource: %s", cr.GetName()) | ||
item, err := apiruntime.DefaultUnstructuredConverter.ToUnstructured(cr) | ||
if err != nil { | ||
return err | ||
} | ||
if err = updateOrCreate(context, gvr, &unstructured.Unstructured{Object: item}); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func updateOrCreate(context *UpgradeContext, gvr schema.GroupVersionResource, cr *unstructured.Unstructured) error { | ||
_, err := context.Resource(gvr).Update(context, cr, metav1.UpdateOptions{}) | ||
if err != nil && apierrors.IsNotFound(err) { | ||
Log("resource not found and create: %s", cr.GetName()) | ||
_, err = context.Resource(gvr).Create(context, cr, metav1.CreateOptions{}) | ||
} | ||
return err | ||
} |
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,29 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
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 hook | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func Log(format string, a ...any) { | ||
fmt.Printf(format, a...) | ||
if !strings.HasSuffix(format, "\n") { | ||
fmt.Println("") | ||
} | ||
} |
Oops, something went wrong.