This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Gitlabbinding #1177
Merged
Merged
Gitlabbinding #1177
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package gitlab | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"path/filepath" | ||
|
||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
const ( | ||
AccessTokenKey = "accessToken" | ||
VolumeName = "gitlab-binding" | ||
MountPath = "/var/bindings/gitlab" | ||
) | ||
|
||
// ReadKey may be used to read keys from the secret bound by the GitLabBinding. | ||
func ReadKey(key string) (string, error) { | ||
data, err := ioutil.ReadFile(filepath.Join(MountPath, key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} | ||
|
||
// AccessToken reads the file named accessToken that is mounted by the GitLabBinding. | ||
func AccessToken() (string, error) { | ||
return ReadKey(AccessTokenKey) | ||
} | ||
|
||
// New instantiates a new gitlab client from the access token from the GitLabBinding | ||
func New(ctx context.Context) (*gitlab.Client, error) { | ||
at, err := AccessToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return gitlab.NewClient(http.DefaultClient, at), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
bindingv1alpha1 "knative.dev/eventing-contrib/gitlab/pkg/apis/bindings/v1alpha1" | ||
sourcev1alpha1 "knative.dev/eventing-contrib/gitlab/pkg/apis/sources/v1alpha1" | ||
"knative.dev/eventing-contrib/gitlab/pkg/reconciler/binding" | ||
"knative.dev/eventing/pkg/logconfig" | ||
"knative.dev/pkg/configmap" | ||
"knative.dev/pkg/controller" | ||
"knative.dev/pkg/injection" | ||
"knative.dev/pkg/injection/sharedmain" | ||
"knative.dev/pkg/signals" | ||
"knative.dev/pkg/webhook" | ||
"knative.dev/pkg/webhook/certificates" | ||
"knative.dev/pkg/webhook/psbinding" | ||
"knative.dev/pkg/webhook/resourcesemantics" | ||
"knative.dev/pkg/webhook/resourcesemantics/defaulting" | ||
"knative.dev/pkg/webhook/resourcesemantics/validation" | ||
) | ||
|
||
var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{ | ||
sourcev1alpha1.SchemeGroupVersion.WithKind("GitLabSource"): &sourcev1alpha1.GitLabSource{}, | ||
bindingv1alpha1.SchemeGroupVersion.WithKind("GitLabBinding"): &bindingv1alpha1.GitLabBinding{}, | ||
} | ||
|
||
var callbacks = map[schema.GroupVersionKind]validation.Callback{} | ||
|
||
func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl { | ||
return defaulting.NewAdmissionController(ctx, | ||
|
||
// Name of the resource webhook. | ||
"defaulting.webhook.gitlab.sources.knative.dev", | ||
|
||
// The path on which to serve the webhook. | ||
"/defaulting", | ||
|
||
// The resources to validate and default. | ||
types, | ||
|
||
// A function that infuses the context passed to Validate/SetDefaults with custom metadata. | ||
func(ctx context.Context) context.Context { | ||
// Here is where you would infuse the context with state | ||
// (e.g. attach a store with configmap data) | ||
return ctx | ||
}, | ||
|
||
// Whether to disallow unknown fields. | ||
true, | ||
) | ||
} | ||
|
||
func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl { | ||
return validation.NewAdmissionController(ctx, | ||
|
||
// Name of the resource webhook. | ||
"validation.webhook.gitlab.sources.knative.dev", | ||
|
||
// The path on which to serve the webhook. | ||
"/resource-validation", | ||
|
||
// The resources to validate and default. | ||
types, | ||
|
||
// A function that infuses the context passed to Validate/SetDefaults with custom metadata. | ||
func(ctx context.Context) context.Context { | ||
// Here is where you would infuse the context with state | ||
// (e.g. attach a store with configmap data) | ||
return ctx | ||
}, | ||
|
||
// Whether to disallow unknown fields. | ||
true, | ||
|
||
// Extra validating callbacks to be applied to resources. | ||
callbacks, | ||
) | ||
} | ||
|
||
func NewGitLabBindingWebhook(opts ...psbinding.ReconcilerOption) injection.ControllerConstructor { | ||
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl { | ||
return psbinding.NewAdmissionController(ctx, | ||
|
||
// Name of the resource webhook. | ||
"gitlabbindings.webhook.gitlab.sources.knative.dev", | ||
|
||
// The path on which to serve the webhook. | ||
"/gitlabbindings", | ||
|
||
// How to get all the Bindables for configuring the mutating webhook. | ||
binding.ListAll, | ||
|
||
// A function that infuses the context passed to Validate/SetDefaults with custom metadata. | ||
func(ctx context.Context, _ psbinding.Bindable) (context.Context, error) { | ||
// Here is where you would infuse the context with state | ||
// (e.g. attach a store with configmap data) | ||
return ctx, nil | ||
}, | ||
opts..., | ||
) | ||
} | ||
} | ||
|
||
func main() { | ||
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{ | ||
ServiceName: "gitlab-source-webhook", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aha, you've named it |
||
Port: 8443, | ||
SecretName: "gitlabsource-webhook-certs", | ||
}) | ||
|
||
glbSelector := psbinding.WithSelector(psbinding.ExclusionSelector) | ||
if os.Getenv("GITLAB_BINDING_SELECTION_MODE") == "inclusion" { | ||
glbSelector = psbinding.WithSelector(psbinding.InclusionSelector) | ||
} | ||
|
||
sharedmain.WebhookMainWithContext(ctx, logconfig.WebhookName(), | ||
certificates.NewController, | ||
NewDefaultingAdmissionController, | ||
NewValidationAdmissionController, | ||
|
||
binding.NewController, NewGitLabBindingWebhook(glbSelector), | ||
) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome! One realization I had this morning (and was chatting with @lberk about) was aligning the changes we make to the environment with existing tooling. e.g. in the case of GitHubBinding, I think we should support the
hub
CLI. Not sure if there is a Gitlab equivalent, or if it's auth is flexible enough to just trivially add this, but here's the Github issue: #1185