Skip to content

Commit

Permalink
fix: metrics server
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jan 26, 2024
1 parent de318c7 commit dd34f62
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 125 deletions.
47 changes: 39 additions & 8 deletions pkg/metricsapiservice/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"time"

"github.com/loft-sh/vcluster/pkg/setup/options"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"k8s.io/metrics/pkg/apis/metrics"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -73,21 +75,50 @@ func deleteOperation(ctrlCtx *options.ControllerContext) wait.ConditionWithConte

func createOperation(ctrlCtx *options.ControllerContext) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
spec := apiregistrationv1.APIServiceSpec{
Group: metrics.GroupName,
GroupPriorityMinimum: 100,
Version: MetricsVersion,
VersionPriority: 100,
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "metrics-service",
Namespace: "kube-system",
},
}
_, err := controllerutil.CreateOrUpdate(ctx, ctrlCtx.VirtualManager.GetClient(), service, func() error {
service.Spec.Type = corev1.ServiceTypeExternalName
service.Spec.ExternalName = "localhost"
service.Spec.Ports = []corev1.ServicePort{
{
Port: 8443,
},
}
return nil
})
if err != nil {
if kerrors.IsAlreadyExists(err) {
return true, nil
}

klog.Errorf("error creating api service %v", err)
return false, nil
}

apiServiceSpec := apiregistrationv1.APIServiceSpec{
Service: &apiregistrationv1.ServiceReference{
Namespace: "kube-system",
Name: "metrics-service",
Port: ptr.To(int32(8443)),
},
InsecureSkipTLSVerify: true,
Group: metrics.GroupName,
GroupPriorityMinimum: 100,
Version: MetricsVersion,
VersionPriority: 100,
}
apiService := &apiregistrationv1.APIService{
ObjectMeta: metav1.ObjectMeta{
Name: MetricsAPIServiceName,
},
}

_, err := controllerutil.CreateOrUpdate(ctx, ctrlCtx.VirtualManager.GetClient(), apiService, func() error {
apiService.Spec = spec
_, err = controllerutil.CreateOrUpdate(ctx, ctrlCtx.VirtualManager.GetClient(), apiService, func() error {
apiService.Spec = apiServiceSpec
return nil
})
if err != nil {
Expand Down
Loading

0 comments on commit dd34f62

Please sign in to comment.