/
flux_source.go
111 lines (95 loc) · 3.23 KB
/
flux_source.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
/*
Copyright 2024. projectsveltos.io. All rights reserved.
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 controllers
import (
"context"
"fmt"
"os"
"github.com/fluxcd/pkg/http/fetch"
"github.com/fluxcd/pkg/tar"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
)
func prepareFileSystemWithFluxSource(source sourcev1.Source, logger logr.Logger) (string, error) {
if source.GetArtifact() == nil {
msg := "Source is not ready, artifact not found"
logger.V(logs.LogInfo).Info(msg)
return "", fmt.Errorf("%s", msg)
}
// Update status with the reconciliation progress.
// revision := source.GetArtifact().Revision
// Create tmp dir.
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("kustomization-%s", source.GetArtifact().Revision))
if err != nil {
err = fmt.Errorf("tmp dir error: %w", err)
return "", err
}
artifactFetcher := fetch.New(
fetch.WithRetries(1),
fetch.WithMaxDownloadSize(tar.UnlimitedUntarSize),
fetch.WithUntar(tar.WithMaxUntarSize(tar.UnlimitedUntarSize)),
fetch.WithHostnameOverwrite(os.Getenv("SOURCE_CONTROLLER_LOCALHOST")))
// Download artifact and extract files to the tmp dir.
err = artifactFetcher.Fetch(source.GetArtifact().URL, source.GetArtifact().Digest, tmpDir)
if err != nil {
return "", err
}
return tmpDir, nil
}
func getSource(ctx context.Context, c client.Client, namespace, sourceName, sourceKind string,
) (client.Object, error) {
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: sourceName,
}
switch sourceKind {
case sourcev1.GitRepositoryKind:
var repository sourcev1.GitRepository
err := c.Get(ctx, namespacedName, &repository)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("unable to get source '%s': %w", namespacedName, err)
}
return &repository, nil
case sourcev1b2.OCIRepositoryKind:
var repository sourcev1b2.OCIRepository
err := c.Get(ctx, namespacedName, &repository)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("unable to get source '%s': %w", namespacedName, err)
}
return &repository, nil
case sourcev1b2.BucketKind:
var bucket sourcev1b2.Bucket
err := c.Get(ctx, namespacedName, &bucket)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("unable to get source '%s': %w", namespacedName, err)
}
return &bucket, nil
default:
return nil, fmt.Errorf("source `%s` kind '%s' not supported",
sourceName, sourceKind)
}
}