Skip to content

Commit

Permalink
feat: operator 支持 httpsd 服务发现机制 (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx authored Sep 3, 2024
1 parent 273ca0a commit 92a2f6f
Show file tree
Hide file tree
Showing 37 changed files with 2,182 additions and 1,698 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,27 @@
// 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 compressor
package gzip

import (
"bytes"
"compress/gzip"
"io"
)

type Compressor interface {
Compress(b []byte) ([]byte, error)
Uncompress(b []byte) ([]byte, error)
}

func Compress(b []byte) ([]byte, error) {
return defaultCompressor.Compress(b)
}

func Uncompress(b []byte) ([]byte, error) {
return defaultCompressor.Uncompress(b)
}

var defaultCompressor = gzipCompressor{}

type gzipCompressor struct{}

func (gzipCompressor) Compress(b []byte) ([]byte, error) {
buf := &bytes.Buffer{}
w := gzip.NewWriter(buf)
if _, err := w.Write(b); err != nil {
w.Close()
_ = w.Close()
return nil, err
}
w.Close()
_ = w.Close()
return buf.Bytes(), nil
}

func (gzipCompressor) Uncompress(conf []byte) ([]byte, error) {
reader := bytes.NewReader(conf)
func Uncompress(b []byte) ([]byte, error) {
reader := bytes.NewReader(b)
r, err := gzip.NewReader(reader)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// 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 compressor
package gzip

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/common/k8sutils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewTkexClient(host string, tlsConfig *rest.TLSClientConfig) (tkexversiond.I
}

func WaitForNamedCacheSync(ctx context.Context, controllerName string, inf cache.SharedIndexInformer) bool {
return operator.WaitForNamedCacheSync(ctx, controllerName, new(logconf.Logger), inf)
return operator.WaitForNamedCacheSync(ctx, controllerName, logconf.New(controllerName), inf)
}

func CreateOrUpdateSecret(ctx context.Context, secretClient clientv1.SecretInterface, desired *corev1.Secret) error {
Expand Down
26 changes: 5 additions & 21 deletions pkg/operator/common/logconf/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,18 @@ import (
)

const (
confStdoutPath = "log.stdout"
confFormatPath = "log.format"
confFileNamePath = "log.filename"
confMaxAgePath = "log.max_age"
confMaxSizePath = "log.max_size"
confMaxBackupPath = "log.max_backup"
confLogLevelPath = "log.level"
confLoggerLevelPath = "logger.level"
)

func initConfig() {
viper.SetDefault(confStdoutPath, false)
viper.SetDefault(confFormatPath, "logfmt")
viper.SetDefault(confFileNamePath, "bkmonitor-operator.log")
viper.SetDefault(confMaxAgePath, 3)
viper.SetDefault(confMaxSizePath, 512)
viper.SetDefault(confMaxBackupPath, 5)
viper.SetDefault(confLogLevelPath, "error")
viper.SetDefault(confLoggerLevelPath, "info")
}

func updateConfig() {
logger.SetOptions(logger.Options{
Stdout: viper.GetBool(confStdoutPath),
Format: viper.GetString(confFormatPath),
Filename: viper.GetString(confFileNamePath),
MaxAge: viper.GetInt(confMaxAgePath),
MaxSize: viper.GetInt(confMaxSizePath),
MaxBackups: viper.GetInt(confMaxBackupPath),
Level: viper.GetString(confLogLevelPath),
Stdout: true,
Format: "logfmt",
Level: viper.GetString(confLoggerLevelPath),
})
}

Expand Down
35 changes: 31 additions & 4 deletions pkg/operator/common/logconf/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,38 @@

package logconf

import "github.com/TencentBlueKing/bkmonitor-datalink/pkg/utils/logger"
import (
"bytes"

type Logger struct{}
"github.com/go-kit/log"

"github.com/TencentBlueKing/bkmonitor-datalink/pkg/utils/logger"
)

type Logger struct {
prefix string
w log.Logger
}

func New(prefix string) *Logger {
l := &Logger{
w: log.NewLogfmtLogger(writer{
prefix: prefix,
}),
}
return l
}

func (l *Logger) Log(keyvals ...interface{}) error {
logger.Debug(keyvals...)
return nil
return l.w.Log(keyvals...)
}

type writer struct {
prefix string
}

func (w writer) Write(b []byte) (int, error) {
s := string(bytes.TrimSpace(b))
logger.Infof("%s\t%s", w.prefix, s)
return len(s), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// 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 utils
package stringx

import "strings"

Expand Down
16 changes: 7 additions & 9 deletions pkg/operator/common/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import (
"fmt"
)

const (
StatefulSetTaskSecretPrefix = "statefulset-worker"
DaemonSetTaskSecretPrefix = "daemonset-worker"
EventTaskSecretPrefix = "event-worker"
)

const (
LabelTaskType = "taskType"

PrefixStatefulSetTaskSecret = "statefulset-worker"
PrefixDaemonSetTaskSecret = "daemonset-worker"
PrefixEventTaskSecret = "event-worker"

TaskTypeDaemonSet = "daemonset"
TaskTypeEvent = "event"
TaskTypeStatefulSet = "statefulset"
Expand All @@ -36,15 +34,15 @@ func ValidateTaskType(t string) bool {
}

func GetDaemonSetTaskSecretName(s string) string {
return fmt.Sprintf("%s-%s", DaemonSetTaskSecretPrefix, s)
return fmt.Sprintf("%s-%s", PrefixDaemonSetTaskSecret, s)
}

func GetStatefulSetTaskSecretName(i int) string {
return fmt.Sprintf("%s-%d", StatefulSetTaskSecretPrefix, i)
return fmt.Sprintf("%s-%d", PrefixStatefulSetTaskSecret, i)
}

func GetEventTaskSecretName() string {
return fmt.Sprintf("%s-0", EventTaskSecretPrefix)
return fmt.Sprintf("%s-0", PrefixEventTaskSecret)
}

func GetTaskLabelSelector(s string) string {
Expand Down
2 changes: 0 additions & 2 deletions pkg/operator/config/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ func InitConfig() error {
return err
}

fmt.Printf("using config file: %s\n", viper.ConfigFileUsed())
fmt.Printf("settings: %+v\n", viper.AllSettings())
EventBus.Publish(EventConfigPostParse)
return nil
}
2 changes: 1 addition & 1 deletion pkg/operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/cespare/xxhash/v2 v2.2.0
github.com/ghodss/yaml v1.0.0
github.com/go-kit/log v0.2.1
github.com/valyala/bytebufferpool v1.0.0
)

Expand Down Expand Up @@ -74,7 +75,6 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/garyburd/redigo v1.6.2 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
3 changes: 1 addition & 2 deletions pkg/operator/operator/dataidwatcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ func (w *dataIDWatcher) deleteDataID(dataID *bkv1beta1.DataID) {
return
}

logger.Infof("delete DataID, name=%v, id=%v, labels=%v", dataID.Name, dataID.Spec.DataID, dataID.Labels)
Publish()
Publish() // 发布信号
}

func (w *dataIDWatcher) deleteMetricDataID(dataID *bkv1beta1.DataID) {
Expand Down
Loading

0 comments on commit 92a2f6f

Please sign in to comment.