代码拉取完成,页面将自动刷新
package certsexpiration
import (
"context"
"encoding/json"
"reflect"
"strings"
"time"
"github.com/rancher/kontainer-engine/cluster"
"github.com/rancher/rancher/pkg/controllers/management/clusterprovisioner"
rkecluster "github.com/rancher/rke/cluster"
"github.com/rancher/rke/pki"
v1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/cert"
)
type Controller struct {
ClusterName string
ClusterLister v3.ClusterLister
ClusterClient v3.ClusterInterface
ClusterStore cluster.PersistentStore
SecretLister v1.SecretLister
}
func Register(ctx context.Context, userContext *config.UserContext) {
c := &Controller{
ClusterName: userContext.ClusterName,
ClusterLister: userContext.Management.Management.Clusters("").Controller().Lister(),
ClusterClient: userContext.Management.Management.Clusters(""),
ClusterStore: clusterprovisioner.NewPersistentStore(userContext.Management.Core.Namespaces(""), userContext.Management.Core),
SecretLister: userContext.Core.Secrets("").Controller().Lister(),
}
userContext.Management.Management.Clusters("").AddHandler(ctx, "certificate-expiration", c.sync)
}
func (c Controller) sync(key string, cluster *v3.Cluster) (runtime.Object, error) {
if key == "" || cluster == nil || cluster.DeletionTimestamp != nil || cluster.Name != c.ClusterName {
return cluster, nil
}
if cluster.Spec.RancherKubernetesEngineConfig == nil {
return cluster, nil
}
certsExpInfo := map[string]v3.CertExpiration{}
cluster, err := c.ClusterLister.Get("", key)
if err != nil {
return cluster, err
}
certBundle, err := c.getClusterCertificateBundle(cluster.Name)
if err != nil {
return cluster, err
}
for certName, certObj := range certBundle {
info, err := getCertExpiration(certObj.CertificatePEM)
if err != nil {
logrus.Debugf("failed to get expiration date for certificate [%s] for cluster [%s]:%v", certName, key, err)
continue
}
certsExpInfo[certName] = info
}
if !reflect.DeepEqual(cluster.Status.CertificatesExpiration, certsExpInfo) {
toUpdate := cluster.DeepCopy()
toUpdate.Status.CertificatesExpiration = certsExpInfo
return c.ClusterClient.Update(toUpdate)
}
return cluster, nil
}
func (c Controller) getClusterCertificateBundle(clusterName string) (map[string]pki.CertificatePKI, error) {
// cluster has a state file ?
currentState, err := getRKECurrentStateFromStore(c.ClusterStore, clusterName)
if err != nil {
return nil, err
}
if currentState != nil {
cleanCertificateBundle(currentState.CertificatesBundle)
return currentState.CertificatesBundle, nil
}
// No state file, let's try get the certs from the user cluster
certs, err := c.getCertsFromUserCluster()
if err != nil {
return nil, err
}
cleanCertificateBundle(certs)
return certs, nil
}
func getRKECurrentStateFromStore(store cluster.PersistentStore, clusterName string) (*rkecluster.State, error) {
cluster, err := store.Get(clusterName)
if err != nil {
return nil, err
}
var fullState rkecluster.FullState
stateStr, ok := cluster.Metadata["fullState"]
if !ok {
return nil, nil
}
err = json.Unmarshal([]byte(stateStr), &fullState)
if err != nil {
return nil, err
}
return &fullState.CurrentState, nil
}
func (c Controller) getCertsFromUserCluster() (map[string]pki.CertificatePKI, error) {
certs := map[string]pki.CertificatePKI{}
secrets, err := c.SecretLister.List("kube-system", labels.Everything())
if err != nil {
return nil, err
}
for _, secret := range secrets {
if strings.HasPrefix(secret.GetName(), "kube-") &&
secret.Type == corev1.SecretTypeOpaque {
cert, ok := secret.Data["Certificate"]
if !ok {
logrus.Debugf("secret [%s] for cluster [%s] doesn't contain a certificate", secret.GetName(), c.ClusterName)
continue
}
certs[secret.GetName()] = pki.CertificatePKI{CertificatePEM: string(cert)}
}
}
return certs, nil
}
func cleanCertificateBundle(certs map[string]pki.CertificatePKI) {
for name := range certs {
if strings.Contains(name, "client") ||
strings.Contains(name, "token") ||
strings.Contains(name, "header") ||
strings.Contains(name, "admin") {
delete(certs, name)
}
}
}
func getCertificateExpDate(c string) (*time.Time, error) {
certs, err := cert.ParseCertsPEM([]byte(c))
if err != nil {
return nil, err
}
return &certs[0].NotAfter, nil
}
func getCertExpiration(c string) (v3.CertExpiration, error) {
date, err := getCertificateExpDate(c)
if err != nil {
return v3.CertExpiration{}, err
}
return v3.CertExpiration{
ExpirationDate: date.Format(time.RFC3339),
}, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。