Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #44 from heptio/sanitizeClusterName
Browse files Browse the repository at this point in the history
Sanitize cluster-name
  • Loading branch information
alexbrand authored Apr 20, 2018
2 parents 19cd13a + 877ecb8 commit 091704c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
5 changes: 3 additions & 2 deletions discovery/cmd/kubernetes-discoverer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ func main() {
}

// Verify cluster name is passed
if clusterName == "" {
log.Fatalf("The Kubernetes cluster name must be provided using the `--cluster-name` flag")
if util.IsInvalidClusterName(clusterName) {
log.Fatalf("The Kubernetes cluster name must be provided using the `--cluster-name` flag or the one passed is invalid")
}
log.Infof("ClusterName is: %s", clusterName)

// Discovered cluster is passed
if discovererKubeCfgFile == "" {
Expand Down
6 changes: 4 additions & 2 deletions discovery/cmd/openstack-discoverer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ func main() {
discovererMetrics = localmetrics.NewMetrics()
discovererMetrics.RegisterPrometheus()

if clusterName == "" {
log.Fatal("The OpenStack cluster name must be provided using the --cluster-name flag")
// Verify cluster name is passed
if util.IsInvalidClusterName(clusterName) {
log.Fatalf("The Kubernetes cluster name must be provided using the `--cluster-name` flag or the one passed is invalid")
}
log.Infof("ClusterName is: %s", clusterName)

gimbalKubeClient, err := k8s.NewClient(gimbalKubeCfgFile, log)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion discovery/pkg/util/log_format.go → discovery/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ limitations under the License.

package util

import "github.com/sirupsen/logrus"
import (
"log"
"regexp"

"github.com/sirupsen/logrus"
)

// GetFormatter returns a textformatter to customize logs
func GetFormatter() *logrus.TextFormatter {
return &logrus.TextFormatter{
FullTimestamp: true,
}
}

// IsInvalidClusterName returns true if valid cluster name
func IsInvalidClusterName(clustername string) bool {
matched, err := regexp.MatchString("^[a-z]([-a-z0-9]*[a-z0-9])?$", clustername)
if err != nil {
log.Fatal(err)
}
return !matched
}
85 changes: 85 additions & 0 deletions discovery/pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright © 2018 Heptio
// 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 util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTranslateService(t *testing.T) {
tests := []struct {
name string
clusterName string
expected bool
}{
{
name: "empty string",
clusterName: "",
expected: true,
},
{
name: "simple",
clusterName: "mycluster",
expected: false,
},
{
name: "hyphen",
clusterName: "my-cluster",
expected: false,
},
{
name: "underscore",
clusterName: "my_cluster",
expected: true,
},
{
name: "multiple underscores",
clusterName: "my----cluster",
expected: false,
},
{
name: "can't start with underscores",
clusterName: "-mycluster",
expected: true,
},
{
name: "can't end with underscores",
clusterName: "mycluster-",
expected: true,
},
{
name: "special chars",
clusterName: "!@!mycl^%$uster**",
expected: true,
},
{
name: "special chars with hyphen & underscore",
clusterName: "!@!my-cl^%$ust_er**",
expected: true,
},
{
name: "whitespace",
clusterName: " my cluster ",
expected: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := IsInvalidClusterName(tc.clusterName)
assert.EqualValues(t, tc.expected, got)
})
}
}
2 changes: 1 addition & 1 deletion docs/kubernetes-discoverer.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Arguments are available to customize the discoverer, most have defaults but othe
| num-threads | 2 | Specify number of threads to use when processing queue items
| gimbal-kubecfg-file | "" | Location of kubecfg file for access to Kubernetes cluster hosting Gimbal
| discover-kubecfg-file | "" | Location of kubecfg file for access to remote Kubernetes cluster to watch for services / endpoints
| cluster-name | "" | Name of cluster scraping for services & endpoints
| cluster-name | "" | Name of cluster scraping for services & endpoints (Cannot start or end with a hyphen and must be lowercase alpha-numeric)
| debug | false | Enable debug logging

### Credentials
Expand Down
2 changes: 1 addition & 1 deletion docs/openstack-discoverer.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Arguments are available to customize the discoverer, most have defaults but othe
| version | false | Show version, build information and quit
| num-threads | 2 | Specify number of threads to use when processing queue items
| gimbal-kubecfg-file | "" | Location of kubecfg file for access to Kubernetes cluster hosting Gimbal
| cluster-name | "" | Name of cluster scraping for services & endpoints
| cluster-name | "" | Name of cluster scraping for services & endpoints (Cannot start or end with a hyphen and must be lowercase alpha-numeric)
| debug | false | Enable debug logging
| reconciliation-period | 30s | The interval of time between reconciliation loop runs
| http-client-timeout | 5s | The HTTP client request timeout
Expand Down

0 comments on commit 091704c

Please sign in to comment.