Skip to content

Commit 1687dec

Browse files
authored
Merge pull request #7 from PDOK/PDOK-17553-Atom-crd_converter-v2-v3
Pdok 17553 atom crd converter v2 v3
2 parents 96044bd + 6b2c950 commit 1687dec

File tree

12 files changed

+174
-152
lines changed

12 files changed

+174
-152
lines changed

api/v2beta1/atom_conversion.go

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"log"
3030
"strconv"
31+
"strings"
3132
"time"
3233

3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -42,47 +43,44 @@ func (src *Atom) ConvertTo(dstRaw conversion.Hub) error {
4243
log.Printf("ConvertTo: Converting Atom from Spoke version v2beta1 to Hub version v3;"+
4344
"source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name)
4445

45-
host := "https://service.pdok.nl/" // Todo read from flag
46-
4746
// ObjectMeta
4847
dst.ObjectMeta = src.ObjectMeta
4948

5049
// Lifecycle
51-
log.Printf("Start mapping the Lifecycle specs...")
5250
if src.Spec.Kubernetes != nil && src.Spec.Kubernetes.Lifecycle != nil && src.Spec.Kubernetes.Lifecycle.TTLInDays != nil {
5351
dst.Spec.Lifecycle.TTLInDays = GetInt32Pointer(int32(*src.Spec.Kubernetes.Lifecycle.TTLInDays))
5452
}
55-
log.Printf("Done mapping the Lifecycle specs...")
5653

5754
// Service
58-
log.Printf("Start mapping the Service...")
5955
dst.Spec.Service = pdoknlv3.Service{
60-
BaseURL: createBaseURL(host, src.Spec.General),
61-
Lang: "nl",
62-
Stylesheet: "https://service.pdok.nl/atom/style/style.xsl",
63-
Title: src.Spec.Service.Title,
64-
Subtitle: src.Spec.Service.Subtitle,
65-
OwnerInfoRef: "pdok",
66-
Links: []pdoknlv3.Link{},
67-
ServiceMetadataLinks: []pdoknlv3.MetadataLink{}, // Todo
68-
Rights: src.Spec.Service.Rights,
56+
BaseURL: createBaseURL(pdoknlv3.GetAtomBaseURLHost(), src.Spec.General),
57+
Lang: "nl",
58+
Stylesheet: "https://service.pdok.nl/atom/style/style.xsl",
59+
Title: src.Spec.Service.Title,
60+
Subtitle: src.Spec.Service.Subtitle,
61+
OwnerInfoRef: "pdok",
62+
ServiceMetadataLinks: pdoknlv3.MetadataLink{
63+
MetadataIdentifier: src.Spec.Service.MetadataIdentifier,
64+
Templates: []string{"csw", "opensearch", "html"},
65+
},
66+
Rights: src.Spec.Service.Rights,
6967
}
70-
log.Printf("Done mapping the Service...")
7168

7269
dst.Spec.DatasetFeeds = []pdoknlv3.DatasetFeed{}
73-
log.Printf("Start mapping the Datasets...")
7470
for _, srcDataset := range src.Spec.Service.Datasets {
7571
dstDatasetFeed := pdoknlv3.DatasetFeed{
76-
TechnicalName: srcDataset.Name,
77-
Title: srcDataset.Title,
78-
Subtitle: srcDataset.Subtitle,
79-
DatasetMetadataLinks: []pdoknlv3.MetadataLink{}, // Todo
72+
TechnicalName: srcDataset.Name,
73+
Title: srcDataset.Title,
74+
Subtitle: srcDataset.Subtitle,
75+
DatasetMetadataLinks: pdoknlv3.MetadataLink{
76+
MetadataIdentifier: srcDataset.MetadataIdentifier,
77+
Templates: []string{"csw", "html"},
78+
},
8079
SpatialDatasetIdentifierCode: srcDataset.SourceIdentifier,
8180
SpatialDatasetIdentifierNamespace: "http://www.pdok.nl",
8281
}
8382

8483
// Map the links
85-
log.Printf("Start mapping the Links...")
8684
for _, srcLink := range srcDataset.Links {
8785
dstLink := pdoknlv3.Link{
8886
Title: srcLink.Type,
@@ -97,10 +95,8 @@ func (src *Atom) ConvertTo(dstRaw conversion.Hub) error {
9795

9896
dstDatasetFeed.Links = append(dstDatasetFeed.Links, dstLink)
9997
}
100-
log.Printf("Done mapping the Links...")
10198

10299
// Map the entries
103-
log.Printf("Start mapping the Entries...")
104100
for _, srcDownload := range srcDataset.Downloads {
105101
dstEntry := pdoknlv3.Entry{
106102
TechnicalName: srcDownload.Name,
@@ -135,7 +131,6 @@ func (src *Atom) ConvertTo(dstRaw conversion.Hub) error {
135131
}
136132

137133
// Map the links
138-
log.Printf("Start mapping the DownloadLinks...")
139134
for _, srcLink := range srcDownload.Links {
140135
dstDownloadLink := pdoknlv3.DownloadLink{}
141136

@@ -162,15 +157,12 @@ func (src *Atom) ConvertTo(dstRaw conversion.Hub) error {
162157

163158
dstEntry.DownloadLinks = append(dstEntry.DownloadLinks, dstDownloadLink)
164159
}
165-
log.Printf("Done mapping the DownloadLinks...")
166160

167161
dstDatasetFeed.Entries = append(dstDatasetFeed.Entries, dstEntry)
168162
}
169-
log.Printf("Done mapping the Entries...")
170163

171164
dst.Spec.DatasetFeeds = append(dst.Spec.DatasetFeeds, dstDatasetFeed)
172165
}
173-
log.Printf("Done mapping the Datasets...")
174166

175167
return nil
176168
}
@@ -185,7 +177,6 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
185177
dst.ObjectMeta = src.ObjectMeta
186178

187179
// General
188-
log.Printf("Start mapping the General specs...")
189180
dst.Spec.General = General{
190181
Dataset: src.ObjectMeta.Labels["dataset"],
191182
DatasetOwner: src.ObjectMeta.Labels["dataset-owner"],
@@ -202,10 +193,7 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
202193
dst.Spec.General.Theme = &theme
203194
}
204195

205-
log.Printf("Done mapping the General specs...")
206-
207196
// Service
208-
log.Printf("Start mapping the Service...")
209197
dst.Spec.Service = AtomService{
210198
Title: src.Spec.Service.Title,
211199
Subtitle: src.Spec.Service.Subtitle,
@@ -214,11 +202,10 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
214202
Name: "PDOK Beheer",
215203
216204
},
205+
MetadataIdentifier: src.Spec.Service.ServiceMetadataLinks.MetadataIdentifier,
217206
}
218-
log.Printf("Done mapping the Service...")
219207

220208
// Datasets
221-
log.Printf("Start mapping the Datasets...")
222209
dst.Spec.Service.Datasets = []Dataset{}
223210
for _, srcDatasetFeed := range src.Spec.DatasetFeeds {
224211
dstDataset := Dataset{
@@ -229,7 +216,6 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
229216
}
230217

231218
// Map the links
232-
log.Printf("Start mapping the Links...")
233219
for _, srcLink := range srcDatasetFeed.Links {
234220
dstDataset.Links = append(dstDataset.Links, OtherLink{
235221
Type: srcLink.Title,
@@ -238,7 +224,6 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
238224
Language: &srcLink.Hreflang,
239225
})
240226
}
241-
log.Printf("Done mapping the Links...")
242227

243228
if len(srcDatasetFeed.Entries) > 0 && srcDatasetFeed.Entries[0].Polygon != nil {
244229
// We can assume all entries have the same bbox, so we take the first one
@@ -252,7 +237,6 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
252237
}
253238

254239
// Map the downloads
255-
log.Printf("Start mapping the Entries...")
256240
for _, srcEntry := range srcDatasetFeed.Entries {
257241
dstDownload := Download{
258242
Name: srcEntry.TechnicalName,
@@ -273,9 +257,7 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
273257
}
274258

275259
// Map the links
276-
log.Printf("Start mapping the DownloadLinks...")
277260
for _, srcDownloadLink := range srcEntry.DownloadLinks {
278-
279261
dstLink := Link{
280262
BlobKey: &srcDownloadLink.Data,
281263
Rel: &srcDownloadLink.Rel,
@@ -297,29 +279,23 @@ func (dst *Atom) ConvertFrom(srcRaw conversion.Hub) error {
297279
}
298280
}
299281

300-
log.Printf("Done mapping the DownloadLinks...")
301282
dstDataset.Downloads = append(dstDataset.Downloads, dstDownload)
302283
}
303-
log.Printf("Done mapping the Entries...")
304284
dst.Spec.Service.Datasets = append(dst.Spec.Service.Datasets, dstDataset)
305285
}
306-
log.Printf("Start mapping the Datasets...")
307286

308287
// Kubernetes
309-
log.Printf("Start mapping the Kubernetes Specs...")
310288
dst.Spec.Kubernetes = &Kubernetes{
311289
Lifecycle: &Lifecycle{},
312290
}
313291
if src.Spec.Lifecycle.TTLInDays != nil {
314292
dst.Spec.Kubernetes.Lifecycle.TTLInDays = GetIntPointer(int(*src.Spec.Lifecycle.TTLInDays))
315293
}
316-
log.Printf("Done mapping the Kubernetes Specs...")
317294

318295
return nil
319296
}
320297

321298
func createBaseURL(host string, general General) (baseURL string) {
322-
323299
atomURI := fmt.Sprintf("%s/%s", general.DatasetOwner, general.Dataset)
324300
if general.Theme != nil {
325301
atomURI += fmt.Sprintf("/%s", *general.Theme)
@@ -330,7 +306,7 @@ func createBaseURL(host string, general General) (baseURL string) {
330306
atomURI += fmt.Sprintf("/%s", *general.ServiceVersion)
331307
}
332308

333-
baseURL = fmt.Sprintf("%s/%s/index.xml", host, atomURI)
309+
baseURL = fmt.Sprintf("%s/%s/index.xml", strings.TrimSuffix(host, "/"), atomURI)
334310
return
335311
}
336312

api/v3/atom_types.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
)
3030

31+
// BaseURLHost is accessed by other api versions (i.e. v2beta1)
32+
var baseURLHost string
33+
3134
// AtomSpec defines the desired state of Atom.
3235
type AtomSpec struct {
3336
Lifecycle Lifecycle `json:"lifecycle,omitempty"`
@@ -42,15 +45,14 @@ type Lifecycle struct {
4245

4346
// Service defines the service configuration for the Atom feed
4447
type Service struct {
45-
BaseURL string `json:"baseUrl"`
46-
Lang string `json:"lang,omitempty"`
47-
Stylesheet string `json:"stylesheet,omitempty"`
48-
Title string `json:"title"`
49-
Subtitle string `json:"subtitle,omitempty"`
50-
OwnerInfoRef string `json:"ownerInfoRef"`
51-
ServiceMetadataLinks []MetadataLink `json:"serviceMetadataLinks,omitempty"`
52-
Links []Link `json:"links,omitempty"` // Todo kan weg?
53-
Rights string `json:"rights,omitempty"`
48+
BaseURL string `json:"baseUrl"`
49+
Lang string `json:"lang,omitempty"`
50+
Stylesheet string `json:"stylesheet,omitempty"`
51+
Title string `json:"title"`
52+
Subtitle string `json:"subtitle,omitempty"`
53+
OwnerInfoRef string `json:"ownerInfoRef"`
54+
ServiceMetadataLinks MetadataLink `json:"serviceMetadataLinks,omitempty"`
55+
Rights string `json:"rights,omitempty"`
5456
}
5557

5658
// Link represents a link in the service or dataset feed
@@ -72,18 +74,18 @@ type Author struct {
7274

7375
// DatasetFeed represents individual dataset feeds within the Atom service
7476
type DatasetFeed struct {
75-
TechnicalName string `json:"technicalName"`
76-
Title string `json:"title"`
77-
Subtitle string `json:"subtitle,omitempty"`
78-
Links []Link `json:"links,omitempty"` // Todo kan weg?
79-
DatasetMetadataLinks []MetadataLink `json:"datasetMetadataLinks,omitempty"`
80-
Author Author `json:"author,omitempty"`
81-
SpatialDatasetIdentifierCode string `json:"spatial_dataset_identifier_code,omitempty"`
82-
SpatialDatasetIdentifierNamespace string `json:"spatial_dataset_identifier_namespace,omitempty"`
83-
Entries []Entry `json:"entries,omitempty"`
84-
}
85-
86-
// Metadatalink represents a link in the service or dataset feed
77+
TechnicalName string `json:"technicalName"`
78+
Title string `json:"title"`
79+
Subtitle string `json:"subtitle,omitempty"`
80+
Links []Link `json:"links,omitempty"` // Todo kan weg?
81+
DatasetMetadataLinks MetadataLink `json:"datasetMetadataLinks,omitempty"`
82+
Author Author `json:"author,omitempty"`
83+
SpatialDatasetIdentifierCode string `json:"spatial_dataset_identifier_code,omitempty"`
84+
SpatialDatasetIdentifierNamespace string `json:"spatial_dataset_identifier_namespace,omitempty"`
85+
Entries []Entry `json:"entries,omitempty"`
86+
}
87+
88+
// MetadataLink represents a link in the service or dataset feed
8789
type MetadataLink struct {
8890
MetadataIdentifier string `json:"metadataIdentifier"`
8991
Templates []string `json:"templates,omitempty"`
@@ -170,3 +172,12 @@ type AtomList struct {
170172
func init() {
171173
SchemeBuilder.Register(&Atom{}, &AtomList{})
172174
}
175+
176+
// SetAtomBaseURLHost is used to set the BaseURL Host in main
177+
func SetAtomBaseURLHost(atomBaseURLHost string) {
178+
baseURLHost = atomBaseURLHost
179+
}
180+
181+
func GetAtomBaseURLHost() string {
182+
return baseURLHost
183+
}

api/v3/zz_generated.deepcopy.go

Lines changed: 2 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
// to ensure that exec-entrypoint and run can make use of them.
2727
_ "k8s.io/client-go/plugin/pkg/client/auth"
2828

29+
traefikiov1alpha1 "github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/traefikio/v1alpha1"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -55,6 +56,8 @@ func init() {
5556

5657
utilruntime.Must(pdoknlv3.AddToScheme(scheme))
5758
utilruntime.Must(pdoknlv2beta1.AddToScheme(scheme))
59+
utilruntime.Must(traefikiov1alpha1.AddToScheme(scheme))
60+
5861
// +kubebuilder:scaffold:scheme
5962
}
6063

@@ -67,6 +70,7 @@ func main() {
6770
var probeAddr string
6871
var secureMetrics bool
6972
var enableHTTP2 bool
73+
var atomBaseURLHost string
7074
var tlsOpts []func(*tls.Config)
7175
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
7276
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
@@ -85,6 +89,9 @@ func main() {
8589
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
8690
flag.BoolVar(&enableHTTP2, "enable-http2", false,
8791
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
92+
93+
flag.StringVar(&atomBaseURLHost, "atom-baseurl-host", "http://localhost:32788/",
94+
"The host which is used to create the Atom BaseURL.")
8895
opts := zap.Options{
8996
Development: true,
9097
}
@@ -93,6 +100,8 @@ func main() {
93100

94101
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
95102

103+
pdoknlv3.SetAtomBaseURLHost(atomBaseURLHost)
104+
96105
// if the enable-http2 flag is false (the default), http/2 should be disabled
97106
// due to its vulnerabilities. More specifically, disabling http/2 will
98107
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and

0 commit comments

Comments
 (0)