Skip to content

Commit

Permalink
Automated Config Connector import.
Browse files Browse the repository at this point in the history
  - e5a0563fa43c7962c32bd7a358a99e4af4a91c6a Use go:embed to embed servicemappings by Config Connector Team <[email protected]>

GitOrigin-RevId: e5a0563fa43c7962c32bd7a358a99e4af4a91c6a
  • Loading branch information
Config Connector Team authored and copybara-github committed Dec 12, 2022
1 parent 6de92f8 commit 1bd18b9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 507 deletions.
1 change: 1 addition & 0 deletions build/builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ENV GOFLAGS "-mod=vendor"
WORKDIR /go/src/github.com/GoogleCloudPlatform/k8s-config-connector
COPY pkg/ pkg/
COPY cmd/ cmd/
COPY config/ config/
COPY vendor/ vendor/
COPY scripts/generate-third-party-licenses scripts/generate-third-party-licenses
COPY scripts/resource-autogen scripts/resource-autogen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration
// +build integration

package main
package servicemappings

import (
"embed"
"fmt"
"log"
"net/http"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/embed"
cnrmvfsgen "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/vfsgen"
"github.com/shurcooL/vfsgen"
)

func main() {
var inputFS http.FileSystem = cnrmvfsgen.ConsistentModTimeFileSystem{
HttpFS: embed.AssetsDir,
//go:embed *.yaml
var servicemappings embed.FS

func ServiceMapping(key string) ([]byte, error) {
b, err := servicemappings.ReadFile(key)
if err != nil {
return nil, fmt.Errorf("error reading embedded file %q: %w", key, err)
}
err := vfsgen.Generate(inputFS, embed.VfsgenOptions)
return b, nil
}

func AllKeys() ([]string, error) {
p := "."
entries, err := servicemappings.ReadDir(p)
if err != nil {
log.Fatalln(fmt.Sprintf("error generating embedded service mappings: %v", err))
return nil, fmt.Errorf("error reading embedded directory %q: %w", p, err)
}
var keys []string
for _, entry := range entries {
keys = append(keys, entry.Name())
}
return keys, nil
}
403 changes: 0 additions & 403 deletions pkg/servicemapping/embed/assets_vfsdata.go

This file was deleted.

42 changes: 0 additions & 42 deletions pkg/servicemapping/embed/embed.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/servicemapping/embed/main/generate.go

This file was deleted.

41 changes: 13 additions & 28 deletions pkg/servicemapping/servicemappingloader/servicemappingloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ package servicemappingloader

import (
"fmt"
"io"
"io/ioutil"
"path"

"github.com/GoogleCloudPlatform/k8s-config-connector/config/servicemappings"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/embed"
autogenloader "github.com/GoogleCloudPlatform/k8s-config-connector/scripts/resource-autogen/servicemapping/servicemappingloader"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -177,15 +174,9 @@ func getLocationalityOfResource(u *unstructured.Unstructured) (string, error) {
}

func GetServiceMappings() ([]v1alpha1.ServiceMapping, error) {
baseDirName := "/"
smDir, err := embed.Assets.Open(baseDirName)
keys, err := servicemappings.AllKeys()
if err != nil {
return nil, fmt.Errorf("error reading files in ServiceMapping directory: %v", err)
}
defer smDir.Close()
files, err := smDir.Readdir(0)
if err != nil {
return nil, fmt.Errorf("error reading files in ServiceMapping directory: %v", err)
return nil, fmt.Errorf("error listing servicemappings: %w", err)
}

autoGenSMMap, err := autogenloader.GetServiceMappingMap()
Expand All @@ -194,9 +185,8 @@ func GetServiceMappings() ([]v1alpha1.ServiceMapping, error) {
}

serviceMappings := make([]v1alpha1.ServiceMapping, 0)
for _, file := range files {
smPath := path.Join(baseDirName, file.Name())
sm, err := fileToServiceMapping(smPath)
for _, key := range keys {
sm, err := fileToServiceMapping(key)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,27 +218,22 @@ func GetServiceMappings() ([]v1alpha1.ServiceMapping, error) {
return serviceMappings, nil
}

func fileToServiceMapping(filePath string) (*v1alpha1.ServiceMapping, error) {
file, err := embed.Assets.Open(filePath)
func fileToServiceMapping(key string) (*v1alpha1.ServiceMapping, error) {
b, err := servicemappings.ServiceMapping(key)
if err != nil {
return nil, fmt.Errorf("error opening file '%v': %v", filePath, err)
return nil, fmt.Errorf("error reading servicemapping %q: %w", key, err)
}
defer file.Close()
sm, err := readerToServiceMapping(file)
sm, err := parseServiceMapping(b)
if err != nil {
return nil, fmt.Errorf("error reading file '%v' to service mapping: %v", filePath, err)
return nil, fmt.Errorf("error parsing %q to service mapping: %w", key, err)
}
return sm, nil
}

func readerToServiceMapping(r io.Reader) (*v1alpha1.ServiceMapping, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
func parseServiceMapping(b []byte) (*v1alpha1.ServiceMapping, error) {
var sm v1alpha1.ServiceMapping
if err := yaml.Unmarshal(bytes, &sm); err != nil {
return nil, fmt.Errorf("error unmarshaling byte to service mapping: %v", err)
if err := yaml.Unmarshal(b, &sm); err != nil {
return nil, fmt.Errorf("error unmarshaling byte to service mapping: %w", err)
}
return &sm, nil
}
Expand Down

0 comments on commit 1bd18b9

Please sign in to comment.