-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add manager package and do some housekeeping (#11)
- Manager package constructs a `ctrl.Manager` for both integration tests and production use, and owns indices since they are manager-scoped (so shouldn't be owned by controllers) - Converts `PodCreation` a pointer because there will be a period of time between allocating the struct and populating the creation timestamp - Adds `LastRolloutTime` to synthesizer status in anticipation of the rollout mechanism - Adds a custom log constructor to add controller name to the logs of each controller. This is better than just adding the fields within the `Reconcile` function because they will also be included when controller-runtime logs errors. - Adds k8s version matrix to the integration tests in CI
- Loading branch information
Showing
14 changed files
with
193 additions
and
82 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
apiv1 "github.com/Azure/eno/api/v1" | ||
) | ||
|
||
const ( | ||
IdxSlicesByCompositionGeneration = ".metadata.ownerReferences.compositionGen" // see: NewSlicesByCompositionGenerationKey | ||
) | ||
|
||
type Options struct { | ||
Rest *rest.Config | ||
HealthProbeAddr string | ||
MetricsAddr string | ||
} | ||
|
||
func New(logger logr.Logger, opts *Options) (ctrl.Manager, error) { | ||
mgr, err := ctrl.NewManager(opts.Rest, manager.Options{ | ||
Logger: logger, | ||
HealthProbeBindAddress: opts.HealthProbeAddr, | ||
Metrics: server.Options{ | ||
BindAddress: opts.MetricsAddr, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = apiv1.SchemeBuilder.AddToScheme(mgr.GetScheme()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = mgr.GetFieldIndexer().IndexField(context.Background(), &apiv1.ResourceSlice{}, IdxSlicesByCompositionGeneration, func(o client.Object) []string { | ||
slice := o.(*apiv1.ResourceSlice) | ||
owner := metav1.GetControllerOf(slice) | ||
if owner == nil || owner.Kind != "Composition" { | ||
return nil | ||
} | ||
return []string{NewSlicesByCompositionGenerationKey(owner.Name, slice.Spec.CompositionGeneration)} | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return mgr, nil | ||
} | ||
|
||
func NewLogConstructor(mgr ctrl.Manager, controllerName string) func(*reconcile.Request) logr.Logger { | ||
return func(req *reconcile.Request) logr.Logger { | ||
l := mgr.GetLogger().WithValues("controller", controllerName) | ||
if req != nil { | ||
l.WithValues("requestName", req.Name, "requestNamespace", req.Namespace) | ||
} | ||
return l | ||
} | ||
} | ||
|
||
// NewSlicesByCompositionGenerationKey documents the key structure used by IdxSlicesByCompositionGeneration. | ||
func NewSlicesByCompositionGenerationKey(compName string, compGeneration int64) string { | ||
// keys will not collide because k8s doesn't allow slashes in names | ||
return fmt.Sprintf("%s/%d", compName, compGeneration) | ||
} |
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.