forked from kumahq/kuma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zone): create Zone resources on zone automatically
Signed-off-by: Jakub Dyszkiewicz <[email protected]>
- Loading branch information
1 parent
fac9cd9
commit 32319ca
Showing
14 changed files
with
155 additions
and
83 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
This file was deleted.
Oops, something went wrong.
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
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,78 @@ | ||
package defaults | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
"github.com/sethvargo/go-retry" | ||
|
||
"github.com/kumahq/kuma/pkg/core/resources/apis/system" | ||
"github.com/kumahq/kuma/pkg/core/resources/manager" | ||
"github.com/kumahq/kuma/pkg/core/resources/model" | ||
"github.com/kumahq/kuma/pkg/core/resources/store" | ||
"github.com/kumahq/kuma/pkg/core/runtime/component" | ||
"github.com/kumahq/kuma/pkg/core/user" | ||
kuma_log "github.com/kumahq/kuma/pkg/log" | ||
) | ||
|
||
type ZoneDefaultComponent struct { | ||
ResManager manager.ResourceManager | ||
Extensions context.Context | ||
ZoneName string | ||
} | ||
|
||
var _ component.Component = &ZoneDefaultComponent{} | ||
|
||
func (e *ZoneDefaultComponent) Start(stop <-chan struct{}) error { | ||
ctx, cancelFn := context.WithCancel(user.Ctx(context.Background(), user.ControlPlane)) | ||
go func() { | ||
<-stop | ||
cancelFn() | ||
}() | ||
return retry.Do(ctx, retry.WithMaxDuration(10*time.Minute, retry.NewConstant(5*time.Second)), func(ctx context.Context) error { | ||
logger := kuma_log.AddFieldsFromCtx(log, ctx, e.Extensions) | ||
if err := EnsureOnlyOneZoneExists(ctx, e.ResManager, e.ZoneName, logger); err != nil { | ||
log.V(1).Info("could not ensure that Zone exists. Retrying.", "err", err) | ||
return retry.RetryableError(err) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (e ZoneDefaultComponent) NeedLeaderElection() bool { | ||
return true | ||
} | ||
|
||
func EnsureOnlyOneZoneExists( | ||
ctx context.Context, | ||
resManager manager.ResourceManager, | ||
zoneName string, | ||
logger logr.Logger, | ||
) error { | ||
logger.Info("ensuring Zone resource exists", "name", zoneName) | ||
zones := &system.ZoneResourceList{} | ||
if err := resManager.List(ctx, zones); err != nil { | ||
return errors.Wrap(err, "cannot list zones") | ||
} | ||
exists := false | ||
for _, zone := range zones.Items { | ||
if zone.GetMeta().GetName() == zoneName { | ||
exists = true | ||
} else { | ||
logger.Info("detected Zone resource with different name than Zone CP name. Deleting. This might happen if you change the name of the Zone CP", "name", zoneName) | ||
if err := resManager.Delete(ctx, zone); err != nil { | ||
return errors.Wrap(err, "cannot delete old zone") | ||
} | ||
} | ||
} | ||
if !exists { | ||
logger.Info("creating Zone resource", "name", zoneName) | ||
zone := system.NewZoneResource() | ||
if err := resManager.Create(ctx, zone, store.CreateByKey(zoneName, model.NoMesh)); err != nil { | ||
return err | ||
} | ||
} | ||
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
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,28 @@ | ||
package defaults | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/kumahq/kuma/pkg/core/resources/model/rest/v1alpha1" | ||
"github.com/kumahq/kuma/test/framework/envs/multizone" | ||
) | ||
|
||
func Defaults() { | ||
It("should create a Zone resource on every zone", func() { | ||
for _, zone := range multizone.Zones() { | ||
Eventually(func(g Gomega) { | ||
// when | ||
out, err := zone.GetKumactlOptions().RunKumactlAndGetOutput("get", "zone", zone.ZoneName(), "-ojson") | ||
|
||
// then | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
meta := &v1alpha1.ResourceMeta{} | ||
g.Expect(json.Unmarshal([]byte(out), meta)).To(Succeed()) | ||
g.Expect(meta.Name).To(Equal(zone.ZoneName())) | ||
}, "30s", "1s").Should(Succeed()) | ||
} | ||
}) | ||
} |
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
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.