Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/v1beta1/grafana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type GrafanaSpec struct {
Ingress *IngressNetworkingV1 `json:"ingress,omitempty"`
// Route sets how the ingress object should look like with your grafana instance, this only works in Openshift.
Route *RouteOpenshiftV1 `json:"route,omitempty"`
// HTTPRoute sets how the ingress object should look like with your grafana instance, this only works use gateway api.
HTTPRoute *HTTPRouteV1 `json:"httpRoute,omitempty"`
// Service sets how the service object should look like with your grafana instance, contains a number of defaults.
Service *ServiceV1 `json:"service,omitempty"`
// Version sets the tag of the default image: docker.io/grafana/grafana.
Expand Down
8 changes: 8 additions & 0 deletions api/v1beta1/typeoverrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/strategicpatch"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// +kubebuilder:object:generate=true
Expand Down Expand Up @@ -327,6 +328,13 @@ type RouteOpenshiftV1 struct {
Spec *RouteOpenShiftV1Spec `json:"spec,omitempty"`
}

// +kubebuilder:object:generate=true

type HTTPRouteV1 struct {
ObjectMeta ObjectMeta `json:"metadata,omitempty"`
Spec gwapiv1.HTTPRouteSpec `json:"spec,omitempty"`
}

// RouteTargetReference specifies the target that resolve into endpoints. Only the 'Service'
// kind is allowed. Use 'weight' field to emphasize one over others.
type RouteTargetReference struct {
Expand Down
22 changes: 22 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,139 changes: 3,139 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanas.yaml

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ rules:
- patch
- update
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
- gateways
verbs:
- get
- list
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
- httproutes
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- grafana.integreatly.org
resources:
Expand Down
15 changes: 15 additions & 0 deletions controllers/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@ var _ AutoDetect = (*autoDetect)(nil)
// AutoDetect provides an assortment of routines that auto-detect traits based on the runtime.
type AutoDetect interface {
IsOpenshift() (bool, error)
HasGatewayAPI() (bool, error)
}

type autoDetect struct {
dcl discovery.DiscoveryInterface
}

func (a *autoDetect) HasGatewayAPI() (bool, error) {
apiGroupList, err := a.dcl.ServerGroups()
if err != nil {
return false, err
}
apiGroups := apiGroupList.Groups
for _, apiGroup := range apiGroups {
if apiGroup.Name == "gateway.networking.k8s.io" {
return true, nil
}
}
return false, nil
}

// New creates a new auto-detection worker, using the given client when talking to the current cluster.
func New(restConfig *rest.Config) (AutoDetect, error) {
dcl, err := discovery.NewDiscoveryClientForConfig(restConfig)
Expand Down
40 changes: 40 additions & 0 deletions controllers/autodetect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,43 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) {
assert.Equal(t, tt.expected, plt)
}
}

func Test_autoDetect_HasGatewayAPI(t *testing.T) {
for _, tt := range []struct {
apiGroupList *metav1.APIGroupList
expected bool
}{
{
&metav1.APIGroupList{},
false,
},
{
&metav1.APIGroupList{
Groups: []metav1.APIGroup{
{
Name: "gateway.networking.k8s.io",
},
},
},
true,
},
} {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
output, err := json.Marshal(tt.apiGroupList)
assert.NoError(t, err)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(output)
assert.NoError(t, err)
}))
defer server.Close()

autoDetect, err := autodetect.New(&rest.Config{Host: server.URL})
require.NoError(t, err)

plt, err := autoDetect.HasGatewayAPI()
require.NoError(t, err)
assert.Equal(t, tt.expected, plt)
}
}
2 changes: 2 additions & 0 deletions controllers/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type GrafanaReconciler struct {
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups="",resources=configmaps;secrets;serviceaccounts;services;persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;

func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := logf.FromContext(ctx).WithName("GrafanaReconciler")
Expand Down
14 changes: 14 additions & 0 deletions controllers/model/grafana_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

func GetCommonLabels() map[string]string {
Expand Down Expand Up @@ -114,6 +115,19 @@ func GetGrafanaIngress(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v12.
return ingress
}

func GetGrafanaHTTPRoute(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *gwapiv1.HTTPRoute {
httpRoute := &gwapiv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-httproute", cr.Name),
Namespace: cr.Namespace,
Labels: GetCommonLabels(),
},
}
controllerutil.SetControllerReference(cr, httpRoute, scheme) //nolint:errcheck

return httpRoute
}

func GetGrafanaRoute(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *routev1.Route {
route := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading