-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ruben Vargas <[email protected]>
- Loading branch information
1 parent
0a595ac
commit af27b51
Showing
8 changed files
with
310 additions
and
20 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
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,30 @@ | ||
package root | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
|
||
configv1alpha1 "github.com/grafana/tempo-operator/api/config/v1alpha1" | ||
) | ||
|
||
var errConfigFileLoading = errors.New("could not read file at path") | ||
|
||
func loadConfigFile(scheme *runtime.Scheme, outConfig *configv1alpha1.ProjectConfig, configFile string) error { | ||
content, err := os.ReadFile(filepath.Clean(configFile)) | ||
if err != nil { | ||
return fmt.Errorf("%w %s", errConfigFileLoading, configFile) | ||
} | ||
|
||
codecs := serializer.NewCodecFactory(scheme) | ||
|
||
if err = runtime.DecodeInto(codecs.UniversalDecoder(), content, outConfig); err != nil { | ||
return fmt.Errorf("could not decode file into runtime.Object: %w", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package root | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/pprof" | ||
"reflect" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
|
||
configv1alpha1 "github.com/grafana/tempo-operator/api/config/v1alpha1" | ||
) | ||
|
||
// LoadConfig initializes the controller configuration, optionally overriding the defaults | ||
// from a provided configuration file. | ||
func LoadConfig(scheme *runtime.Scheme, configFile string) (*configv1alpha1.ProjectConfig, ctrl.Options, error) { | ||
options := ctrl.Options{Scheme: scheme} | ||
ctrlConfig := configv1alpha1.DefaultProjectConfig() | ||
|
||
if configFile == "" { | ||
return &ctrlConfig, options, nil | ||
} | ||
|
||
err := loadConfigFile(scheme, &ctrlConfig, configFile) | ||
if err != nil { | ||
return nil, options, fmt.Errorf("failed to parse controller manager config file: %w", err) | ||
} | ||
|
||
options = mergeOptionsFromFile(options, &ctrlConfig) | ||
return &ctrlConfig, options, nil | ||
} | ||
|
||
func mergeOptionsFromFile(o manager.Options, cfg *configv1alpha1.ProjectConfig) manager.Options { | ||
o = setLeaderElectionConfig(o, cfg.ControllerManagerConfigurationSpec) | ||
|
||
if o.Metrics.BindAddress == "" && cfg.Metrics.BindAddress != "" { | ||
o.Metrics.BindAddress = cfg.Metrics.BindAddress | ||
|
||
endpoints := map[string]http.HandlerFunc{ | ||
"/debug/pprof/": pprof.Index, | ||
"/debug/pprof/cmdline": pprof.Cmdline, | ||
"/debug/pprof/profile": pprof.Profile, | ||
"/debug/pprof/symbol": pprof.Symbol, | ||
"/debug/pprof/trace": pprof.Trace, | ||
} | ||
|
||
if o.Metrics.ExtraHandlers == nil { | ||
o.Metrics.ExtraHandlers = map[string]http.Handler{} | ||
} | ||
|
||
for path, handler := range endpoints { | ||
o.Metrics.ExtraHandlers[path] = handler | ||
} | ||
} | ||
|
||
if o.HealthProbeBindAddress == "" && cfg.Health.HealthProbeBindAddress != "" { | ||
o.HealthProbeBindAddress = cfg.Health.HealthProbeBindAddress | ||
} | ||
|
||
if cfg.Webhook.Port != nil { | ||
o.WebhookServer = webhook.NewServer(webhook.Options{ | ||
Port: *cfg.Webhook.Port, | ||
}) | ||
} | ||
|
||
return o | ||
} | ||
|
||
func setLeaderElectionConfig(o manager.Options, obj configv1alpha1.ControllerManagerConfigurationSpec) manager.Options { | ||
if obj.LeaderElection == nil { | ||
// The source does not have any configuration; noop | ||
return o | ||
} | ||
|
||
if !o.LeaderElection && obj.LeaderElection.LeaderElect != nil { | ||
o.LeaderElection = *obj.LeaderElection.LeaderElect | ||
} | ||
|
||
if o.LeaderElectionResourceLock == "" && obj.LeaderElection.ResourceLock != "" { | ||
o.LeaderElectionResourceLock = obj.LeaderElection.ResourceLock | ||
} | ||
|
||
if o.LeaderElectionNamespace == "" && obj.LeaderElection.ResourceNamespace != "" { | ||
o.LeaderElectionNamespace = obj.LeaderElection.ResourceNamespace | ||
} | ||
|
||
if o.LeaderElectionID == "" && obj.LeaderElection.ResourceName != "" { | ||
o.LeaderElectionID = obj.LeaderElection.ResourceName | ||
} | ||
|
||
if o.LeaseDuration == nil && !reflect.DeepEqual(obj.LeaderElection.LeaseDuration, metav1.Duration{}) { | ||
o.LeaseDuration = &obj.LeaderElection.LeaseDuration.Duration | ||
} | ||
|
||
if o.RenewDeadline == nil && !reflect.DeepEqual(obj.LeaderElection.RenewDeadline, metav1.Duration{}) { | ||
o.RenewDeadline = &obj.LeaderElection.RenewDeadline.Duration | ||
} | ||
|
||
if o.RetryPeriod == nil && !reflect.DeepEqual(obj.LeaderElection.RetryPeriod, metav1.Duration{}) { | ||
o.RetryPeriod = &obj.LeaderElection.RetryPeriod.Duration | ||
} | ||
|
||
return o | ||
} |
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.