generated from knative-extensions/sample-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
145 lines (122 loc) · 4.25 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
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 github
import (
"context"
listers "github.com/n3wscott/gateway/pkg/client/listers/gateway/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"
"knative.dev/pkg/system"
"github.com/n3wscott/gateway/pkg/apis/gateway/v1alpha1"
reconcilergithub "github.com/n3wscott/gateway/pkg/client/injection/reconciler/gateway/v1alpha1/github"
)
const (
serviceName = "gateway"
)
// newReconciledNormal makes a new reconciler event with event type Normal, and
// reason SlackReconciled.
func newReconciledNormal(namespace, name string) pkgreconciler.Event {
return pkgreconciler.NewEvent(corev1.EventTypeNormal, "GitHubReconciled", "GitHub reconciled: \"%s/%s\"", namespace, name)
}
// Reconciler reconciles a GitHub object
type Reconciler struct {
// KubeClientSet allows us to talk to the k8s for core APIs
kubeClientSet kubernetes.Interface
// listers index properties about resources
githubLister listers.GitHubLister
sinkResolver *resolver.URIResolver
gh Instance
}
// Check that our Reconciler implements Interface
var _ reconcilergithub.Interface = (*Reconciler)(nil)
func getAddressableDestination() duckv1.Destination {
path, _ := apis.ParseURL("/github")
return duckv1.Destination{
Ref: &duckv1.KReference{
Kind: "Service",
Namespace: system.Namespace(),
Name: serviceName,
APIVersion: "v1",
},
URI: path,
}
}
func getSinkDestination(source *v1alpha1.GitHub) duckv1.Destination {
dest := source.Spec.Sink.DeepCopy()
if dest.Ref != nil {
// To call URIFromDestination(), dest.Ref must have a Namespace. If there is
// no Namespace defined in dest.Ref, we will use the Namespace of the source
// as the Namespace of dest.Ref.
if dest.Ref.Namespace == "" {
//TODO how does this work with deprecated fields
dest.Ref.Namespace = source.GetNamespace()
}
}
return *dest
}
// TODO: leverage the instance name and instance annotations.
// ReconcileKind implements Interface.ReconcileKind.
func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1alpha1.GitHub) pkgreconciler.Event {
source.Status.InitializeConditions()
source.Status.ObservedGeneration = source.Generation
// Mark Address
addrURI, err := r.sinkResolver.URIFromDestinationV1(getAddressableDestination(), source)
if err != nil {
source.Status.MarkNoAddress("NotFound", "")
return err
}
source.Status.MarkAddress(addrURI)
// Mark Sink
sinkURI, err := r.sinkResolver.URIFromDestinationV1(getSinkDestination(source), source)
if err != nil {
source.Status.MarkNoSink("NotFound", "")
return err
}
source.Status.MarkSink(sinkURI)
if source.Status.Organization != nil && source.Status.Organization.Login != source.Spec.Organization {
source.Status.Organization = nil
source.Status.Repositories = nil
source.Status.MarkUnknownOrganization("Organization resync progressing.")
return nil
}
if source.Status.Organization == nil {
org, err := r.gh.GetOrganization(ctx, source)
if err != nil {
source.Status.Organization = &v1alpha1.GitHubOrganization{
Login: source.Spec.Organization,
Type: "Error",
}
source.Status.MarkUnknownOrganization("Unable to fetch org. %s", err.Error())
return err
} else if org != nil {
source.Status.Organization = org
source.Status.MarkValidOrganization()
}
}
if source.Status.Repositories == nil {
repos, err := r.gh.GetRepositories(ctx, source)
if err != nil {
source.Status.Repositories = []v1alpha1.GitHubRepository{{
Name: "Error",
}}
return err
} else if repos != nil {
source.Status.Repositories = repos
}
}
return newReconciledNormal(source.Namespace, source.Name)
}