|
| 1 | +/* |
| 2 | +Copyright 2020 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pipelinelooprun |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "strconv" |
| 25 | + "sync" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/IBM/ibm-cos-sdk-go/aws" |
| 29 | + "github.com/IBM/ibm-cos-sdk-go/aws/credentials" |
| 30 | + "github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam" |
| 31 | + "github.com/IBM/ibm-cos-sdk-go/aws/session" |
| 32 | + "github.com/IBM/ibm-cos-sdk-go/service/s3" |
| 33 | + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + "k8s.io/client-go/kubernetes" |
| 35 | + "knative.dev/pkg/system" |
| 36 | +) |
| 37 | + |
| 38 | +type ObjectStoreLogConfig struct { |
| 39 | + Enable bool |
| 40 | + defaultBucketName string |
| 41 | + accessKey string |
| 42 | + secretKey string |
| 43 | + ibmStyleCredentials bool |
| 44 | + apiKey string |
| 45 | + serviceInstanceID string |
| 46 | + region string |
| 47 | + serviceEndpoint string |
| 48 | + authEndpoint string |
| 49 | + client *s3.S3 |
| 50 | +} |
| 51 | + |
| 52 | +type Logger struct { |
| 53 | + buffer *bytes.Buffer |
| 54 | + // When buffer reaches the size of MaxSize, it tries to sync with object store. |
| 55 | + MaxSize int64 |
| 56 | + // Whether to compress before syncing the buffer. |
| 57 | + Compress bool |
| 58 | + // Current size of the buffer. |
| 59 | + size int64 |
| 60 | + // Sync irrespective of buffer size after elapsing this interval. |
| 61 | + SyncInterval time.Duration |
| 62 | + mu sync.Mutex |
| 63 | + LogConfig *ObjectStoreLogConfig |
| 64 | +} |
| 65 | + |
| 66 | +// ensure we always implement io.WriteCloser |
| 67 | +var _ io.WriteCloser = (*Logger)(nil) |
| 68 | + |
| 69 | +func (l *Logger) Write(p []byte) (n int, err error) { |
| 70 | + l.mu.Lock() |
| 71 | + defer l.mu.Unlock() |
| 72 | + writeLen := int64(len(p)) |
| 73 | + if l.size+writeLen >= l.MaxSize { |
| 74 | + if err := l.syncBuffer(); err != nil { |
| 75 | + return 0, err |
| 76 | + } |
| 77 | + } |
| 78 | + if n, err = l.buffer.Write(p); err != nil { |
| 79 | + return n, err |
| 80 | + } |
| 81 | + l.size = l.size + int64(n) |
| 82 | + return n, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (l *Logger) syncBuffer() error { |
| 86 | + fmt.Printf("Syncing buffer size : %d, MaxSize: %d \n", l.size, l.MaxSize) |
| 87 | + err := l.LogConfig.writeToObjectStore(l.LogConfig.defaultBucketName, |
| 88 | + time.Now().Format(time.RFC3339Nano), l.buffer.Bytes()) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + l.buffer.Reset() |
| 93 | + l.size = 0 |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (l *Logger) Close() error { |
| 98 | + l.mu.Lock() |
| 99 | + defer l.mu.Unlock() |
| 100 | + return l.syncBuffer() |
| 101 | +} |
| 102 | + |
| 103 | +func (o *ObjectStoreLogConfig) load(ctx context.Context, kubeClientSet kubernetes.Interface) error { |
| 104 | + configMap, err := kubeClientSet.CoreV1().ConfigMaps(system.Namespace()). |
| 105 | + Get(ctx, "object-store-config", metaV1.GetOptions{}) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if o.Enable, err = strconv.ParseBool(configMap.Data["enable"]); err != nil || !o.Enable { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if o.ibmStyleCredentials, err = strconv.ParseBool(configMap.Data["ibmStyleCredentials"]); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + o.apiKey = configMap.Data["apiKey"] |
| 118 | + o.accessKey = configMap.Data["accessKey"] |
| 119 | + o.secretKey = configMap.Data["secretKey"] |
| 120 | + o.serviceInstanceID = configMap.Data["serviceInstanceID"] |
| 121 | + o.region = configMap.Data["region"] |
| 122 | + o.serviceEndpoint = configMap.Data["serviceEndpoint"] |
| 123 | + o.authEndpoint = configMap.Data["authEndpoint"] |
| 124 | + o.defaultBucketName = configMap.Data["defaultBucketName"] |
| 125 | + ibmCredentials := ibmiam.NewStaticCredentials(aws.NewConfig(), o.authEndpoint, o.apiKey, o.serviceInstanceID) |
| 126 | + s3Credentials := credentials.NewStaticCredentials(o.accessKey, o.secretKey, "") |
| 127 | + var creds *credentials.Credentials |
| 128 | + if o.ibmStyleCredentials { |
| 129 | + creds = ibmCredentials |
| 130 | + } else { |
| 131 | + creds = s3Credentials |
| 132 | + } |
| 133 | + // Create client config |
| 134 | + var conf = aws.NewConfig(). |
| 135 | + WithRegion(o.region). |
| 136 | + WithEndpoint(o.serviceEndpoint). |
| 137 | + WithCredentials(creds). |
| 138 | + WithS3ForcePathStyle(true) |
| 139 | + |
| 140 | + var sess = session.Must(session.NewSession()) |
| 141 | + o.client = s3.New(sess, conf) |
| 142 | + input := &s3.CreateBucketInput{ |
| 143 | + Bucket: aws.String(o.defaultBucketName), |
| 144 | + } |
| 145 | + _, err = o.client.CreateBucket(input) |
| 146 | + if err != nil { |
| 147 | + fmt.Printf("This error might be harmless, as the default bucket may already exist, %v\n", |
| 148 | + err.Error()) |
| 149 | + } |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (o *ObjectStoreLogConfig) CreateNewBucket(bucketName string) error { |
| 154 | + if !o.Enable || bucketName == o.defaultBucketName { |
| 155 | + return nil |
| 156 | + } |
| 157 | + input := &s3.CreateBucketInput{ |
| 158 | + Bucket: aws.String(bucketName), |
| 159 | + } |
| 160 | + _, err := o.client.CreateBucket(input) |
| 161 | + return err |
| 162 | +} |
| 163 | + |
| 164 | +func (o *ObjectStoreLogConfig) writeToObjectStore(bucketName string, key string, content []byte) error { |
| 165 | + if !o.Enable { |
| 166 | + return nil |
| 167 | + } |
| 168 | + input := s3.PutObjectInput{ |
| 169 | + Bucket: aws.String(bucketName), |
| 170 | + Key: aws.String(key), |
| 171 | + Body: bytes.NewReader(content), |
| 172 | + } |
| 173 | + |
| 174 | + _, err := o.client.PutObject(&input) |
| 175 | + // fmt.Printf("Response from object store: %v\n", obj) |
| 176 | + return err |
| 177 | +} |
| 178 | + |
| 179 | +func (l *Logger) LoadDefaults(ctx context.Context, kubeClientSet kubernetes.Interface) error { |
| 180 | + |
| 181 | + if l.LogConfig == nil { |
| 182 | + l.LogConfig = &ObjectStoreLogConfig{} |
| 183 | + err := l.LogConfig.load(ctx, kubeClientSet) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + if !l.LogConfig.Enable { |
| 188 | + return fmt.Errorf("Object store logging is disabled. " + |
| 189 | + "Please edit `object-store-config` configMap to setup logging.\n") |
| 190 | + } |
| 191 | + } |
| 192 | + if l.buffer == nil { |
| 193 | + l.buffer = new(bytes.Buffer) |
| 194 | + } |
| 195 | + return nil |
| 196 | +} |
0 commit comments